一、設(shè)置導(dǎo)航欄底線
- 簡(jiǎn)單獲取底線
- (UIView *)navLine {
if (!_navLine) {
UIView *backgroundView = [self.navigationController.navigationBar subviews].firstObject;
_navLine = backgroundView.subviews.firstObject;
}
return _navLine;
}
2.單個(gè)頁(yè)面設(shè)置隱藏顯示
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navLine.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.navLine.hidden = NO;
}
二徙鱼、設(shè)置導(dǎo)航欄背景圖
1.設(shè)置導(dǎo)航欄背景圖所需的各個(gè)尺寸
1倍圖 640 * 128 px (一般用不到)
2倍圖 750 * 128 px (5s,6全度,6s, 7)
3倍圖 1242*192 px (6p, 6sp)
備注:其實(shí)一般兩種圖就足夠幔妨,5s也是用2倍圖,現(xiàn)在一般最低適配到5s傍菇,當(dāng)然如果需求不一樣就三張猾瘸。
設(shè)置導(dǎo)航背景圖代碼
/*設(shè)置圖片拉伸范圍 如果圖片被擠壓變形的情況下*/
UIImage *backImage = [UIImage imageNamed:@"homeNav"];
backImage = [backImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 100, 10, 10)];
/*設(shè)置背景圖*/
[self.navigationController.navigationBar setBackgroundImage:backImage forBarMetrics:UIBarMetricsDefault];
設(shè)置導(dǎo)航條背景圖片時(shí)有時(shí)self.view會(huì)向下偏移64像素
//此句代碼解決坐標(biāo)問(wèn)題
[self.navigationController.navigationBar setTranslucent:YES];
//當(dāng)translucent = YES,controller中self.view的原點(diǎn)是從導(dǎo)航欄左上角開(kāi)始計(jì)算
//當(dāng)translucent = NO丢习,controller中self.view的原點(diǎn)是從導(dǎo)航欄左下角開(kāi)始計(jì)算
設(shè)置導(dǎo)航欄背景純色
UINavigationBar *bar = [UINavigationBar appearance];
bar.barTintColor = [UIColor colorWithRed:62/255.0 green:173/255.0 blue:176/255.0 alpha:1.0];
設(shè)置導(dǎo)航欄title字體類型和大小
[self.navigationController.navigationBar setTitleTextAttributes:@{
NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:22],
NSForegroundColorAttributeName:[UIColor whiteColor]
}];
設(shè)置導(dǎo)航欄透明
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];``
[self.navigationController.navigationBar setShadowImage:nil];
}