iOS开发-设置UINavigationBar透明

RT~

方法1

- (void)mk_resetNavigationBarTranslucent {
     
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
}

方法2

- (void)viewWillAppear:(BOOL)animated {
     
    [super viewWillAppear:animated];
    
    [self mk_resetNaviBarWithAlpha:0];
}

- (void)viewWillDisappear:(BOOL)animated {
     
    [super viewWillDisappear:animated];
    
    [self mk_resetNaviBarWithAlpha:0.5];
}

- (void)mk_resetNaviBarWithAlpha:(CGFloat)alpha {
     
    [self.navigationController.navigationBar setBackgroundImage:[self mk_imageWithColor:[UIColor redColor]] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.navigationBar.translucent = alpha == 0;
}

- (UIImage *)mk_imageWithColor:(UIColor *)color {
     
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

你可能感兴趣的:(iOS开发,ios,objective-c)