gpt4 book ai didi

ios5 - UINavigationBar 与纯色 iOS 5

转载 作者:行者123 更新时间:2023-12-01 07:21:00 25 4
gpt4 key购买 nike

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

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

它工作得很好,但在 iOS 5 中不行。我需要导航栏颜色是纯色的,没有任何渐变。我该怎么做?

据我所知,对于 iOS 5,替换 drawRect 的唯一方法是方法是创建一个子类,但是有什么方法可以让所有导航 Controller 都使用 UINavigationBar子类而不是原始类?

最佳答案

在 iOS 5 中,您可以使用 UIAppearance 协议(protocol)来设置应用程序中所有 UINavigationBars 的外观。引用:link .

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

在您的应用程序委托(delegate)中,执行以下操作:

UIImage *image = [UIImage imageNamed:@"navbarbg.png"];
[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

您还必须添加 UIAppearanceContainer头文件的协议(protocol):
@interface AppDelegate : UIResponder <UIApplicationDelegate, UIAppearanceContainer>

@end

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

关于ios5 - UINavigationBar 与纯色 iOS 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8266182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com