需求:看不到導(dǎo)航欄,圖片置頂

697DED41-E0A7-474D-9764-985CD798266C.png
如果簡單的隱藏導(dǎo)航欄或設(shè)置導(dǎo)航欄背景色為透明的話
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
self.navigationController.navigationBar.hidden = NO;
}
會(huì)出現(xiàn)切換時(shí)候有黑色區(qū)域,如下圖

DCA8741C-609D-44C2-9FF2-0925C360601A.png
解決方法:
1、最簡單直接:注意這里一定要用動(dòng)畫的方式隱藏導(dǎo)航欄,這樣在使用滑動(dòng)返回手勢的時(shí)候效果最好.這樣做有一個(gè)缺點(diǎn)就是在切換tabBar的時(shí)候有一個(gè)導(dǎo)航欄向上消失的動(dòng)畫.
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
2、設(shè)置self為導(dǎo)航控制器的代理,實(shí)現(xiàn)代理方法,在將要顯示控制器中設(shè)置導(dǎo)航欄隱藏和顯示,使用這種方式不僅完美切合滑動(dòng)返回手勢,同時(shí)也解決了切換tabBar的時(shí)候,導(dǎo)航欄動(dòng)態(tài)隱藏的問題.
@interface HMNavigationController ()<UINavigationControllerDelegate>
@end
@implementation HMNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
// 設(shè)置導(dǎo)航控制器的代理為self
self.navigationController.delegate = self;
}
#pragma mark - UINavigationControllerDelegate// 將要顯示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
// 判斷要顯示的控制器是否是自己
BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
[self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
注意:問題來了,如果從一個(gè)隱藏導(dǎo)航欄頁面切換到另一個(gè)隱藏導(dǎo)航欄頁面的話,就會(huì)出現(xiàn)導(dǎo)航條慢慢從左往右出現(xiàn)的畫面

1517D610-B533-4078-B0B6-8D82CE19519B.png
最終解決方法:(導(dǎo)航欄的背景圖用無色透明的圖片,下面是用重新畫的一張圖片,此時(shí)導(dǎo)航欄雖然看不到,實(shí)際上是變成了無色透明的了,其實(shí)還是存在的,而且還可以繼續(xù)設(shè)置UIBarButtonItem)
- (void)setNavigationBar {
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:[UIColor clearColor]]];
}
+ (UIImage *)imageWithColor:(UIColor *)color;{
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context,color.CGColor);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
并且從一個(gè)無導(dǎo)航欄也面切換到另一個(gè)無導(dǎo)航欄頁面也是可以的,效果圖如下:

abc.jpg