iphone - UINavigationBar 更改类别中的色调颜色

标签 iphone ios uinavigationbar

我知道我可以轻松更改 xib 文件中导航栏的色调颜色。但问题是,如果我必须在很多地方进行更改。

因此,我尝试使用(花式)类别并覆盖 draw rect 方法。

#import "UINavigationBar+customTint.h"

@implementation UINavigationBar (customTint)

-(void)drawRect:(CGRect)rect{
    self.tintColor = [UIColor redColor];
}

@end

我也将它导入到相应的 View Controller 中。我究竟做错了什么?
正确的做法是什么?

提前致谢

最佳答案

这是一种方法。你可能会更容易ViewController在需要更改的地方添加以下行:

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

只为一行使用一个类别可能是矫枉过正。

编辑:
如果您确实想使用类别,您可能需要调用 setNeedsDisplay在导航栏上。或覆盖另一个方法并调用它。
就像是 ,
[self.navigationController.navigationBar setNeedsDisplay];

Also according to Apple's documentation

In iOS 5, the UINavigationBar, UIToolbar, and UITabBar implementations have changed so that the drawRect: method is not called unless it is implemented in a subclass. Apps that have re-implemented drawRect: in a category on any of these classes will find that the drawRect: method isn't called. UIKit does link-checking to keep the method from being called in apps linked before iOS 5 but does not support this design on iOS 5 or later. Apps can either:

  • Use the customization API for bars in iOS 5 and later, which is the preferred way.
  • Subclass UINavigationBar (or the other bar classes) and override drawRect: in the subclass.


因此,解决此问题的最佳方法是将其放在您的ApplicationDidFinishLaunching 中。
NSString *reqSysVer = @"5.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
  [[UINavigationBar appearance] setTintColor:myColor];

并留下您的DrawRect , 这样它就可以在低于 5 的 iOS 上运行

关于iphone - UINavigationBar 更改类别中的色调颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9308724/

相关文章:

ios - 检测设备是否支持电话?

ios - 为什么当我点击它们时,我的按钮的文本颜色没有改变?

ios - 创建一个透明的 NavigationBar

ios - 防止 Share Extension 中的 UINavigationBar 继承主应用程序外观设置

ios - 蒙面的 UIVisualEffectView 在 iOS 10 上不起作用

iphone - stringByAddingPercentEscapesUsingEncoding :? 的反义词是什么?

iOS : How to get song id using echonest Api

ios - 替换嵌套 NSDictionary 中出现的 NSNull

ios - 降低 AVAudioPlayer 声音的音量

ios - 为什么将 imageview 添加到 Storyboard中的导航栏会删除导航栏?