设置导航栏透明

// 这个是私有方法不建议
- (void)viewWillAppear:(BOOL)animated{
    // 设置导航栏透明
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    self.navigationController.navigationBar.translucent = YES;

    // 遍历导航栏的子视图,将其背景层设置透明
    for (UIView *subview in self.navigationController.navigationBar.subviews) {
        if ([subview isKindOfClass:NSClassFromString(@"_UIBarBackground")]) {
            subview.backgroundColor = [UIColor clearColor];
            subview.alpha = 0.0; // 隐藏这一层视图
        }
    }
//    self.navigationController.navigationBar.hidden = YES;
}
建议写法
- (void)setClearBackGround {
    if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
        [appearance configureWithOpaqueBackground];
        appearance.titleTextAttributes = @{
            NSFontAttributeName: [UIFont dt_pingfangOfSize:16 weight:UIFontWeightMedium],
            NSForegroundColorAttributeName: [UIColor whiteColor],
        };
        appearance.backgroundColor = [UIColor clearColor];
        appearance.shadowImage = [UIImage imageFromColor:[UIColor clearColor]];
        appearance.backgroundImage = [UIImage imageFromColor:[UIColor clearColor]];
        self.navigationBar.scrollEdgeAppearance = appearance;
        self.navigationBar.standardAppearance = appearance;
        self.navigationBar.compactScrollEdgeAppearance = appearance;
        self.navigationBar.compactAppearance = appearance;
        self.navigationBar.translucent = true;
        self.navigationBar.tintColor = [UIColor whiteColor];
    }
    else {
        //设置导航栏透明
        [self.navigationBar setTranslucent:true];
        //把背景设为空
        [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        //处理导航栏有条线的问题
        [self.navigationBar setShadowImage:[UIImage new]];
        
        // 导航背景设置
        [self.navigationBar setBarTintColor:[UIColor whiteColor]];
        [self.navigationBar setTintColor:[UIColor whiteColor]];
        
        [self.navigationBar setTitleTextAttributes:@{
            NSForegroundColorAttributeName:[UIColor whiteColor],
            NSFontAttributeName:[UIFont dt_pingfangOfSize:16 weight:UIFontWeightMedium]
        }];
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容