設(shè)置導(dǎo)航欄的背景顏色
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:53.0/255.0 green:217.0/255.0 blue:142.0/255.0 alpha:1];
/* 這里不能直接設(shè)置backgroundColor */
設(shè)置后發(fā)現(xiàn)跟設(shè)置的顏色值有偏差,查了下應(yīng)該是上面navigationBar有一層透明視圖的原因沃于,添加如下代碼
The default value is YES. If the navigation bar has a custom background image, the default is YES if any pixel of the image has an alpha value of less than 1.0, and NO otherwise.
If you set this property to YES on a navigation bar with an opaque custom background image, the navigation bar will apply a system opacity less than 1.0 to the image.
If you set this property to NO on a navigation bar with a translucent custom background image, the navigation bar provides an opaque background for the image using black if the navigation bar has UIBarStyleBlack style, white if the navigation bar has UIBarStyleDefault, or the navigation bar’s barTintColor if a custom value is defined.
默認(rèn)為YES涩咖。導(dǎo)航欄有背景圖片的情況下,如過這個(gè)圖片中的任意一個(gè)像素透明度小于1繁莹,則默認(rèn)為YES,否則為NO檩互。
如果當(dāng)導(dǎo)航欄有一個(gè)自定義背景圖片的情況下,你設(shè)置translucent為YES,導(dǎo)航欄會(huì)給這張圖片加上一個(gè)透明度小于1的視圖闸昨。
導(dǎo)航欄在有一個(gè)半透明背景圖片時(shí)候你設(shè)置translucent為NO淹禾,導(dǎo)航欄會(huì)給這個(gè)半透明圖片加上一層不透明的試圖句惯,如果導(dǎo)航欄樣式為UIBarStyleBlack則添加一層黑色試圖,如果導(dǎo)航欄樣式為UIBarStyleDefault或者導(dǎo)航欄已經(jīng)設(shè)置過barTintColor屬性則添加一層白色試圖
self.navigationController.navigationBar.translucent = NO;
一個(gè)坑:原圖上采色計(jì)采集的數(shù)值跟模擬器上顯示出的圖片采集的數(shù)值不一樣不一樣不一樣啊 設(shè)置barTintColor的數(shù)值要在原圖上去采集
原圖:53/217/142
模擬器圖:60/223/153
導(dǎo)航欄顏色:68/227/163 // translucent為YES RGB采集的模擬器上圖片的數(shù)值
導(dǎo)航欄去橫線
橫線理解為UINavegationBar的shadowImage屬性循诉,文檔中這樣說明
For a custom shadow image to be shown, a custom background image must also be set with the setBackgroundImage:forBarMetrics: method. If the default background image is used, then the default shadow image will be used regardless of the value of this property.
大致意思是說要設(shè)置shadowImage必須先要通過
setBackgroundImage:forBarMetrics:方法給UINavgationBar設(shè)置一個(gè)背景圖片
未設(shè)置前
//UIBarMetricsDefault-豎屏橫屏都有,橫屏導(dǎo)航條變寬茄猫,則自動(dòng)repeat圖片
//UIBarMetricsCompact-豎屏沒有,橫屏有
加上下面代碼
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
效果
另一種方法:
@interface ViewController ()
{
UIImageView *navBarHairlineImageView; // 系統(tǒng)默認(rèn)的那條橫線圖片
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationController.navigationBar.translucent = NO;
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:53.0/255.0 green:217.0/255.0 blue:142.0/255.0 alpha:1];
// [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
// [self.navigationController.navigationBar setShadowImage:[UIImage new]];
navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
}
/* 可以在這兩個(gè)方法里控制是否顯示橫條
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
navBarHairlineImageView.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
navBarHairlineImageView.hidden = NO;
}
*/
// 找到那條橫線
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
return (UIImageView *)view;
}
for (UIView *subview in view.subviews) {
UIImageView *imageView = [self findHairlineImageViewUnder:subview];
if (imageView) {
return imageView;
}
}
return nil;
}