更改导航栏的颜色

问题描述:

如何更改导航栏的默认蓝色颜色?更改导航栏的颜色

谢谢。

+0

这是非常接近这个问题:http://*.com/questions/901542/using-image-or-tint-color-on-uinavigationbar-in-iphone – 2009-06-03 12:40:09

UINavigationBar类有一个UIColor *tintColor属性,您可以在代码中进行更改。

或者,此属性也在InterfaceBuilder UI设计工具中公开。

+0

感谢您的回复。如果我可以使用图像作为导航栏背景,请让我知道吗? – ebaccount 2009-06-02 23:34:15

假设您以编程方式添加了导航栏,而不是在Interface Builder中,只需将其放入viewDidLoad方法即可。

self.navigationController.navigationBar.tintColor = [UIColor grayColor]; 

TintColor属性不会影响导航栏的默认子视图作为底部边框和阴影。有时候,重写导航栏的布局是有用的。

尽管navigationBar是UINavigationController的只读属性,但您可以通过“setValue:forKey:”避免 此限制。这个方法在5个成功提交给AppStore的应用程序上获得批准。

您可以继承UINavigationBar并根据需要更改drawRect:方法。 例如,

@implementation CustomNavigationBar 

- (void) drawRect:(CGRect)rect 
{ 
    [super drawRect:rect]; 
    UIImage *backgroundImage = ImageFromColor(WANTED_COLOR); 
    [backgroundImage drawInRect:rect]; 
} 

后,你也可以继承的UINavigationController和更改initWithRootViewController:

- (id) initWithRootViewController:(UIViewController *)rootViewController 
{ 
    self = [super initWithRootViewController:rootViewController]; 
    if (self) 
    { 
     CustomNavigationBar *navBar = [CustomNavigationBar new]; 
     [self setValue:navBar forKey:@"navigationBar"]; 
    } 
    return self; 
} 

你也可以改变由制作类别的UINavigationController和实现方法混写的initWithRootViewController这种方法:

附:昨天我的新应用出现在AppStore,这种方法没有任何问题。