iOS导航栏底部线条隐藏和显示
奇怪的产品,脑洞大开的UI让人欲罢不能.这个页面要下划线,那个页面又不要,弄得我们很尴尬.
前提
项目中要有BaseViewController和BaseNavigationController
BaseNavigationController
Q:为什么要用UIImageView自定义?
A:产品和UI说变就变,为自己以后留一线,谁知道是改颜色还是图片
//寻找底部横线
- (UIImageView *)foundNavigationBarBottomLine:(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 foundNavigationBarBottomLine:subview];
if (imageView) {
return imageView;
}
}
return nil;
}
//显示底部横线
- (void)showNavBarBottomLine {
UIImageView *bottomLine = [self foundNavigationBarBottomLine:self.navigationBar];
if (bottomLine) {
bottomLine.hidden = YES;
}
UIImageView *navLine = [self.navigationBar.subviews[0] viewWithTag:5757];
if (navLine) {
navLine.hidden = NO;
CGRect bottomLineFrame = bottomLine.frame;
bottomLineFrame.origin.y = CGRectGetMaxY(self.navigationBar.frame);
navLine.frame = bottomLineFrame;
}else {
CGRect bottomLineFrame = bottomLine.frame;
bottomLineFrame.origin.y = CGRectGetMaxY(self.navigationBar.frame);
UIImageView *navLine = [[UIImageView alloc] initWithFrame:bottomLineFrame];
navLine.tag = 5757;
navLine.backgroundColor = ColorNavSepGapLine;
if (self.navigationBar.subviews.count) {
[self.navigationBar.subviews[0] addSubview:navLine];
}else{
bottomLine.hidden = NO;
}
}
}
//隐藏底部横线
- (void)hideNavBarBottomLine {
UIImageView *bottomLine = [self foundNavigationBarBottomLine:self.navigationBar];
if (bottomLine) {
bottomLine.hidden = YES;
}
UIImageView *navLine = [self.navigationBar.subviews[0] viewWithTag:5757];
if (navLine) {
navLine.hidden = YES;
}
}
BaseViewController
- (void)showNavBarBottomLine {
if ([self.navigationController isKindOfClass:[BaseNavigationController class]]) {
[(BaseNavigationController *)self.navigationController showNavBarBottomLine];
}
}
- (void)hideNavBarBottomLine {
if ([self.navigationController isKindOfClass:[BaseNavigationController class]]) {
[(BaseNavigationController *)self.navigationController hideNavBarBottomLine];
}
}
使用
最后在每一个控制器的willappear和willdisappear去调用就好了.
网友评论