2012-05-23 3 views
0

이 질문은이 질문이 많은 사람들에게 매우 좋다고 생각합니다. 하지만 iOS 개발자의 초보자인데 개선 중이 라하더라도 혼란 스러울 이유가 있습니다. 내 앱 메뉴에는 탐색 모음이 포함 된 탐색 컨트롤러와 탐색 항목 btw가 포함 된보기 컨트롤러가 포함되어 있습니다. 내 탐색 막대가 UINavigationBar을 하위 클래스로 지정하여 원하는대로 만듭니다.UINavigationControllers/UIBarButtonItems에 대한 몇 가지 정보가 필요합니다.

이제 i (정보)가있는 사용자 지정 BarButtonItem을 추가하려고합니다. 이미 그렸습니다. 그래서 그것은 내 질문입니다 :이 버튼을 어떻게 추가 할 수 있습니까?

귀하의 조언에 대해 감사드립니다.

답변

1
// Create the Info button 
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 

// Give it an action handler 
[infoButton addTarget:self 
       action:@selector(infoButtonAction:) 
    forControlEvents:UIControlEventTouchUpInside]; 

// Create a UIBarButtonItem to "contain" the Info button 
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton]; 

// Add the UIBarButtonItem to the navigation bar (on the left side) 
[self.navigationItem setLeftBarButtonItem:infoItem animated:NO]; 

액션 핸들러 :

- (void)infoButtonAction:(id)sender { 
    NSLog(@"Tapped on info button!"); 
} 
+0

감사합니다 ... 내 ViewController의 viewDidLoad 메서드에서 작성해야합니다 같아요? – Rob

1

이 직접 질문에 대한 답변되지 않을 수도 있습니다,하지만 그것을 사용하는 것을 의미했다 당신이 UINavigationController가 사용하고 있던 경우는 아마 문제를 방지 할 것입니다. 인터페이스를 사용자 정의하기 위해 UINavigationBar의 하위 클래스를 만드는 대신 UIAppearance 메서드를 사용한 다음 일반처럼 UINavigationController를 만듭니다.

// Create navigation controller. 
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController]; 

// Customize navigation bar appearance. 
[[UINavigationBar appearance] setTintColor:[UIColor blueColor]]; 
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation-bar-background"] forBarMetrics:UIBarMetricsDefault]; 
+0

고마워요. 매우 유용합니다. :) – Rob

관련 문제