我知道我可以轻松更改 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/