iOS - 实现导航栏透明的几种方法(总结)

 
 
前言:

在开发中,为了美观很多设计成导航栏透明的样式,下面就列举一下实现导航栏透明的几种方法

第一种方法:
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    for (UIView *aView in self.navigationController.navigationBar.subviews) {

        if ([aView isKindOfClass:NSClassFromString(@"_UINavigationBarBackground")]) {

            aView.hidden = YES;

        }
    }

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

    for (UIView *aView in self.navigationController.navigationBar.subviews) {

        if ([aView isKindOfClass:NSClassFromString(@"_UINavigationBarBackground")]) {
            aView.hidden = NO;
        }

    }

}
第二种方法:
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    //去除 navigationBar 底部的细线
    self.navigationController.navigationBar.shadowImage = [UIImage new];
第三种方法
self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    UIImage *image = [self createAImageWithColor:[UIColor clearColor] alpha:0.0];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
- (UIImage *)createAImageWithColor:(UIColor *)color alpha:(CGFloat)alpha{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextSetAlpha(context, alpha);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}
注意点:

设置导航栏透明的时候,如果在Push到其他的控制器,其他的控制器导航栏也是会变得透明,所以为了防止这类情况的发生,最好在- (void)viewWillDisappear:(BOOL)animated方法中,把导航栏的颜色还原过来



作者:CoderBob
链接:http://www.jianshu.com/p/fddc4ee4bc1f
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值