方法一:
- (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];
}
以上方法,发现有一个bug,在界面间进行push和pop时,导航栏会有闪现;
方法二:
这个设置就能解决上面的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;
//这个颜色的设置是为了填充上因为translucent导致的导航栏背景变成白色,如果你要的就是白色,这个可以忽略;
[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 颜色转图片
+ (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;
}