方法一
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationBar setShadowImage:[self imageWithColor:[UIColor redColor] size:CGSizeMake([UIScreen mainScreen].bounds.size.width, 0.5)]];
#pragma mark - public methods
- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
if (!color || size.width <= 0 || size.height <= 0) return nil;
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
此方法是现在大部分博客中有介绍的,不过有缺陷。如果
self.navigationBar.translucent = YES;
则navigationbar
透明了。有时我们又不得不设self.navigationBar.translucent = YES;
例如使用UISearchController
做通讯录时。其中的坑可以看我这篇博文介绍使用UISearchController时 navigationBar translucent 属性导致的问题总结。
方法二
此方法是我推荐的一种,详情请看代码及代码注释。(不过在iOS11+后不能使用,需要使用方法三)
首先导入头文件
#import <objc/runtime.h>
代码展示
//先查看View层次结构
NSLog(@"Navigationbar recursiveDescription:\n%@",[self.navigationBar performSelector:@selector(recursiveDescription)]);
//打印完后我们发现有个高度为0.5的UIImageView 类型 SuperView type为 _UIBarBackground的视图
//遍历navigationBar 属性
unsigned int outCount = 0;
Ivar *ivars = class_copyIvarList([self.navigationBar class], &outCount);
for (NSInteger i = 0; i < outCount; ++i) {
Ivar ivar = *(ivars + i);
NSLog(@"navigationBar Propertys:\n name = %s \n type = %s", ivar_getName(ivar),ivar_getTypeEncoding(ivar));
}
free(ivars);
//遍历结果可以发现navigationbar 中的type 为_UIBarBackground 名称为 _barBackgroundView
//遍历_barBackgroundView 中的属性
unsigned int viewOutCount = 0;
UIView *barBackgroundView = nil;
/*iOS 10.0+为`_barBackgroundView`,小于iOS10.0这个属性名称为`_UIBarBackground`.*/
if (sysytemVersion<10.0) {
barBackgroundView = [self.navigationBar valueForKey:@"_backgroundView"];
}else{
barBackgroundView = [self.navigationBar valueForKey:@"_barBackgroundView"];
}
if (barBackgroundView) {
Ivar *viewivars = class_copyIvarList([barBackgroundView class], &viewOutCount);
for (NSInteger i = 0; i < viewOutCount; ++i) {
Ivar ivar = *(viewivars + i);
NSLog(@"_barBackgroundView Propertys:\n name = %s \n type = %s", ivar_getName(ivar),ivar_getTypeEncoding(ivar));
}
free(viewivars);
}
//找到type为 UIImageView 的属性有_shadowView,_backgroundImageView。因为底部线条可以设置shadowImage,所有我们猜测是_shadowView
UIImageView *navigationbarLineView = [barBackgroundView valueForKey:@"_shadowView"];
if (navigationbarLineView && [navigationbarLineView isKindOfClass:[UIImageView class]]) {
UIView *lineView = [[UIView alloc]init];
lineView.backgroundColor = [UIColor redColor];
lineView.translatesAutoresizingMaskIntoConstraints = NO;
[navigationbarLineView addSubview:lineView];
//这里我们要用约束不然旋转后有问题
[navigationbarLineView addConstraint:[NSLayoutConstraint constraintWithItem:lineView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:navigationbarLineView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
[navigationbarLineView addConstraint:[NSLayoutConstraint constraintWithItem:lineView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:navigationbarLineView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
[navigationbarLineView addConstraint:[NSLayoutConstraint constraintWithItem:lineView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:navigationbarLineView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
[navigationbarLineView addConstraint:[NSLayoutConstraint constraintWithItem:lineView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:navigationbarLineView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];
}
为了更好的理解,我加了分析过程,实际应用时,可以去掉分析过程。
方法三
UIView *lineView = [[UIView alloc]init];
lineView.backgroundColor = [UIColor redColor];
lineView.translatesAutoresizingMaskIntoConstraints = NO;
[self.navigationBar addSubview:lineView];
UIView *superView = self.navigationBar;
[superView addConstraint:[NSLayoutConstraint constraintWithItem:lineView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
[superView addConstraint:[NSLayoutConstraint constraintWithItem:lineView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
[superView addConstraint:[NSLayoutConstraint constraintWithItem:lineView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];
[lineView addConstraint:[NSLayoutConstraint constraintWithItem:lineView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:1]];
下载链接
扩展
方法二中你可以写个category
添加和移除