UINavigationBar,纯色iOS 5 [英] UINavigationBar with solid color iOS 5
问题描述
我正在使用该类别来自定义导航栏。我的代码是:
I'm using the category to customize nav bar. My code is:
- (void) drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context, CGColorGetComponents([self.tintColor CGColor]));
CGContextFillRect(context, rect);
}
它运作良好,但在iOS 5中不行。我需要导航栏颜色到坚实没有任何渐变。我应该怎么做?
It works well, but not in iOS 5. I need nav bar color to be solid without any gradient. How should I do this?
据我所知,对于iOS 5,替换 drawRect
方法的唯一方法是创建一个子类,但有没有办法让所有导航控制器使用 UINavigationBar
子类而不是原始类?
As I know, for iOS 5 the only way to replace drawRect
method is to make a subclass, but is there any way to make all navigation controllers to use UINavigationBar
subclass instead of original class?
推荐答案
在iOS 5中,您可以使用UIAppearance协议设置应用中所有UINavigationBars的外观。参考:链接。
In iOS 5 you can use the UIAppearance protocol to set the appearance of all UINavigationBars in your app. Reference: link.
我获得纯色的方法是为导航栏创建自定义图像,并将其设置为UINavigationBars背景。您可能必须创建与UINavigationBar大小相同的图像,至少这就是我所做的。
The way I got a solid color was to create a custom image for the navigation bar, and set it as UINavigationBars background. You might have to create the image with the same size as the UINavigationBar, at least that's what I did.
在您的应用委托中,执行以下操作:
In your app delegate, do something like this:
UIImage *image = [UIImage imageNamed:@"navbarbg.png"];
[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
您还必须添加 UIAppearanceContainer
协议到头文件:
You also have to add the UIAppearanceContainer
protocol to the header file:
@interface AppDelegate : UIResponder <UIApplicationDelegate, UIAppearanceContainer>
@end
你可以通过设置一些颜色来达到同样的效果什么的,而不是图像。但我发现这是一个简单的解决方案。
You can probably achieve the same thing by setting some color or something, instead of an image. But I found this to be an easy solution.
这篇关于UINavigationBar,纯色iOS 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!