在我们使用UINavigationBar时,经常需要修改他的背景色,通常我们会写出如下代码
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)]; navBar.backgroundColor = [UIColor redColor];
但是,当我们把它添加到self.view上面时,却发现他的效果并不理想,明明设置的红色,却像是加上了一层蒙版一样,当时我就是百思不得其解,后来经过在网上查阅,使用了一种比较麻烦的方法,写了一个分类,专门给UINavgationBar修改背景色,具体原理这里不做讲述了,因为我想说的是更好的方法,其实官方已经写得很明白了,如下
/* The behavior of tintColor for bars has changed on iOS 7.0. It no longer affects the bar's background and behaves as described for the tintColor property added to UIView. To tint the bar's background, please use -barTintColor. */@property(null_resettable, nonatomic,strong) UIColor *tintColor;//文字颜色(item.left的文字颜色 item.right )@property(nullable, nonatomic,strong) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR; // default is nil背景色
所以,我们应该设置的是navBar.barTintColor = [UIColor redColor];这样就是我们想要的效果了
ps:个人学习中遇到的一点坑,与君共勉,大神请无视