UINavigationBar,纯色iOS 5

Ant*_*nov 3 uinavigationbar customizing ios5

我正在使用该类别来自定义导航栏.我的代码是:

- (void) drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColor(context, CGColorGetComponents([self.tintColor CGColor]));
    CGContextFillRect(context, rect);
}
Run Code Online (Sandbox Code Playgroud)

它运行良好,但不是在iOS 5中.我需要导航栏颜色是实心的,没有任何渐变.我该怎么做?

据我所知,对于iOS 5,替换drawRect方法的唯一方法是创建一个子类,但有没有办法让所有导航控制器使用UINavigationBar子类而不是原始类?

mat*_*tsr 13

在iOS 5中,您可以使用UIAppearance协议设置应用程序中所有UINavigationBars的外观.参考:链接.

我获得纯色的方法是为导航栏创建自定义图像,并将其设置为UINavigationBars背景.您可能必须创建与UINavigationBar大小相同的图像,至少这就是我所做的.

在您的app委托中,执行以下操作:

UIImage *image = [UIImage imageNamed:@"navbarbg.png"];
[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)

您还必须将UIAppearanceContainer协议添加到头文件:

@interface AppDelegate : UIResponder <UIApplicationDelegate, UIAppearanceContainer>

@end
Run Code Online (Sandbox Code Playgroud)

您可以通过设置一些颜色或其他东西而不是图像来实现相同的功能.但我发现这是一个简单的解决方案.