iOS去掉導航欄底部的線
阿新 • • 發佈:2018-08-01
bject ont 去掉 attr nav set 分享圖片 default ive
第一種方法:
UINavigationBar *navigationBar = self.navigationController.navigationBar;
// white.png圖片自己下載個純白色的色塊,或者自己ps做一個
[navigationBar setBackgroundImage:[UIImage imageNamed:@"white.png"]
forBarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
[navigationBar setShadowImage:[UIImage new]];
運行後效果如下(此處原有的灰色背景色會被white.png代替):
PS:這是唯一一個隱藏這條線的官方用法,但是有一個缺陷-刪除了translucency(半透明)
第二種方法:
1)聲明UIImageView變量,存儲底部橫線
@implementation MyViewController {
UIImageView *navBarHairlineImageView;
}
2)在viewDidLoad中加入:
navBarHairlineImageView = [self findHairlineImageViewUnder:navigationBar];
3)實現找出底部橫線的函數
- (UIImageView *)findHairlineImageViewUnder:(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 findHairlineImageViewUnder:subview];
if (imageView) {
return imageView;
}
}
return nil;
}
4)最後在viewWillAppear,viewWillDisappear中處理
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
navBarHairlineImageView.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
navBarHairlineImageView.hidden = NO;
}
PS:第二種方法可以保持bar的translucent
iOS去掉導航欄底部的線