隐藏navigationBar底部的黑线shadowImage

  1. 直接设置navigationBar的shadowImage和backgroundImage, 需先让translucent属性设置为NO

    1
    2
    3
    4
    -(void)removeBlackLine {
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    }
  2. 循环遍历navigationBar的子视图,找到高度小于1的黑线, 在appear是隐藏, disappear时再显示(推荐)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    @implementation ViewController {
    UIImageView *navBarHairlineImageView;
    }
    - (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    navBarHairlineImageView.hidden = YES;
    }
    - (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    navBarHairlineImageView.hidden = NO;
    }
    - (void)viewDidLoad {
    [super viewDidLoad];
    navBarHairlineImageView= [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    // Do any additional setup after loading the view, typically from a nib.
    }
    - (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;
    }
  3. 切去超出部分, 此方法会把状态栏给切掉

    1
    2
    -(void)useClipsToBoundsRemoveBlackLine { self.navigationController.navigationBar.clipsToBounds = YES;  
    }
文章目录