方法一:
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:nil];
}
以上方法,發(fā)現(xiàn)有一個bug,在界面間進行push和pop時,導(dǎo)航欄會有閃現(xiàn);
方法二:
這個設(shè)置就能解決上面的bug
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.subviews[0].alpha = 0.0;
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
self.navigationController.navigationBar.translucent = NO;
self.navigationController.navigationBar.subviews[0].alpha = 1.0;
//這個顏色的設(shè)置是為了填充上因為translucent導(dǎo)致的導(dǎo)航欄背景變成白色,如果你要的就是白色,這個可以忽略;
[self.navigationController.navigationBar setBackgroundImage:[UIColor createImageWithColor:[UIColor initWithR:246 G:246 B:247]] forBarMetrics:UIBarMetricsDefault];
}
上面用到的一個顏色生成圖片的方法:
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIColor (RGB)
#pragma mark 顏色轉(zhuǎn)圖片
+ (UIImage *)createImageWithColor:(UIColor*)color;
@end
+ (UIImage*)createImageWithColor:(UIColor*)color{
CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}