iOS9开发最简单的设置导航栏透明UINavigationBar透明

创建一个继承自UINavigationController的导航控制器

如果想要设置导航栏透明只需要在init方法中添加这几行代码就能搞定

我这里用的是initWithRootViewController:这个就看个人喜好了,重要的是里面的代码。。。

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController {
    self = [super initWithRootViewController:rootViewController];
    if (self) {
        UIView *view = [[UIView alloc] init];
        view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64);
        [self.view insertSubview:view belowSubview:self.navigationBar];
        [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"这里去网上找一个镂空的图片,多大分辨率都行"] forBarMetrics:UIBarMetricsCompact];
        self.navigationBar.layer.masksToBounds = YES;// 去掉横线(没有这一行代码导航栏的最下面还会有一个横线)
    }
    return self;
}

就是这么简单,赶紧去试试。

你可能感兴趣的:(iOS开发)