直接上方法:
1.隐藏黑线:(会影响navigationBar的translucent属性)
- (void)viewWillDisappear:(BOOL)animated{
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:nil];
}
如果不想影响下个界面的效果:
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
}
2.遍历找出这条黑线,再做相应的处理:
先写个方法,用来找出这条黑线:
- (UIImageView *)findBottomLineInView:(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 findBottomLineInView:subview];
if (imageView) {
return imageView;
}
}
return nil;
}
然后再做相应处理:
- (void)viewWillAppear:(BOOL)animated {
UIImageView *navBarBottomLine = [self findBottomLineInView:self.navigationController.navigationBar];
navBarBottomLine.hidden = YES;
}
也可以自定义view直接覆盖:
UIImageView *navLine = [[UIImageView alloc] initWithFrame:navBarBottomLine.frame];
navLine.backgroundColor = [UIColor RedColor];
[self.navigationController.view addSubview:navLine];
navLine.backgroundColor = [UIColor blueColor];
[self.navigationController.view addSubview:navLine];
3.利用setShadowImage:方法
给UIImage写个分类---->用颜色值来创建一张图片
+ (UIImage *)imageWithColor:(UIColor *)color withSize:(CGSize)size
{
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1.0] withSize:CGSizeMake(self.view.frame.size.width, 1)] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:[UIColor colorWithRed:199/255.0 green:199/255.0 blue:199/255.0 alpha:0.5] withSize:CGSizeMake(self.view.frame.size.width, 1)]];
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:50/255.0 green:50/255.0 blue:50/255.0 alpha:1.0]];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:136/255.0 green:240/255.0 blue:0 alpha:1.0] withSize:CGSizeMake(self.view.frame.size.width, 1)] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
}
这样就可以做到分开单独设置或者统一设置