2011-08-01 2 views
1

다음 코드를 사용하여 내 앱에 맞춤 탐색 바를 추가하고 있습니다. 그러나보기에 따라 navbar를 변경할 수 있기를 원하지만 가능합니까? 그렇지 않으면UINavBar를 사용자 지정하는 방법?

@implementation UINavigationBar (UINavigationBarCategory) 
- (void)drawRect:(CGRect)rect 
{ 
    UIColor *color = [UIColor colorWithRed:82/255.0 green:80/255.0 blue:81/255.0 alpha:1.0]; 
    UIImage *img = [UIImage imageNamed: @"navbar.png"]; 
    [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
    self.tintColor = color; 
} 

@end

, 그냥 대신 제목 텍스트의 네비게이션 바에 이미지를 추가 할 수 있습니다?

답변

2

이 디자인이 일 때 -drawRect을 덮어 쓰려면이 카테고리를 사용하지 않는 것이 좋습니다.은 곧 출시 될 iOS 버전에서 중단됩니다.

navBar 제목에 맞춤 이미지와 버튼을 만드는 것은 매우 쉽습니다. 여기에 내 앱 중 하나의 프로덕션 코드에서 직접 가져온 코드 스 니펫이 있습니다. 그것은 네비게이션 바의 제목 영역에서 사용자 지정 이미지 버튼을 생성하지만, 대신 이미지를 만들만큼 쉽게 :

- (void) viewDidLoad { 
    UIButton *titleButton = [self buttonForTitleView]; 
    [titleButton setTitle:newTitle forState:UIControlStateNormal]; 
    [titleButton sizeToFit]; 

    self.navigationBar.topItem.titleView = titleButton; 

} 

- (UIButton *) buttonForTitleView { 
    if (!_buttonForTitleView) { 
     UIImage *buttonImage = [UIImage imageNamed:@"button_collection_name2"]; 
     UIImage *pressedButtonImage = [UIImage imageNamed:@"button_collection_name2_pressed"]; 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [button setBackgroundImage: buttonImage forState : UIControlStateNormal]; 
     [button setBackgroundImage: pressedButtonImage forState : UIControlStateHighlighted]; 
     [button setFrame:CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height)]; 
     [button setTitleColor:[UIColor colorWithRed:38.0/255.0 green:38.0/255.0 blue:38.0/255.0 alpha:1.0] forState:UIControlStateNormal]; 
     [button setTitleShadowColor:[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0] forState:UIControlStateNormal]; 
     button.titleLabel.adjustsFontSizeToFitWidth = YES; 
     button.titleLabel.minimumFontSize = 10.0; 
     button.titleEdgeInsets = UIEdgeInsetsMake(0.0, 10.0, 0.0, 10.0); 
     button.titleLabel.shadowOffset = (CGSize){ 0, 1 }; 
     [button addTarget:self action:@selector(presentWidgetCollectionSwitchingViewController:) forControlEvents:UIControlEventTouchUpInside]; 
     _buttonForTitleView = [button retain]; 
    }  
    return _buttonForTitleView; 
} 

을 그리고이 버튼은 응용 프로그램 VideoBot의 모습입니다 : VideoBot custom navbar title button

여기 네비게이션 바 타이틀 이미지를 사용하는 예 :

UIImage *titleImage = [UIImage imageNamed:@"navbar_title_image"]; 
UIImageView *titleImageView = [[[UIImageView alloc] initWithImage:titleImage] autorelease]; 

UIView *container = [[[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, titleImage.size.width, titleImage.size.height}] autorelease]; 
container.backgroundColor = [UIColor clearColor]; 
[container addSubview:titleImageView]; 

// add the view to the title 
self.navigationBar.topItem.titleView= container;
+0

덕분에, 1 개 관점에서, 나는 거기에 이미지 대신 텍스트를 (내가 이것에 대한 .PNG를 사용할 수 있습니다)가 사용자 정의 메뉴 바를 원한다. 다른 한편으로, 나는 단지 표준 텍스트로 사용자 정의 navbar를 원한다. 어떻게 할 수 있는가? –

+0

버튼, 이미지, 분할 된 컨트롤과 같은 컨테이너보기에 원하는 것을 넣을 수 있습니다. 컨테이너 컨트롤에 UIButton을 추가했지만 쉽게 UIImageView를 추가 할 수 있습니다. – memmons

+0

무슨 뜻인지 확실하지 않습니다. 코드를 볼 수 없으면 이상한 행동을하는 이유를 디버깅해야합니다. – memmons

관련 문제