IOS 改变导航栏颜色

  1. 替换系统自带的返回箭头

 

 UIImage *image = [[UIImage imageNamed:@"leftCancelWhite"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];

 

//箭头的点击事件
- (void)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}
  1. 更换导航栏颜色
    这里需要做兼容,IOS 13以上的系统不支持以前修改颜色的代码

注意这里改变颜色是全局改变

 

UINavigationBar *appearance = [UINavigationBar appearance];
if (@available(iOS 13.0, *)) {
        
        UINavigationBarAppearance *barAppearance = UINavigationBarAppearance.new;
        barAppearance.backgroundColor = [UIColor colorWithHexString:@"#F4895C"];
        UIBarButtonItemStateAppearance *normal = barAppearance.buttonAppearance.normal;
        if (normal) {
            normal.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor grayColor]};
        }
        
        UIBarButtonItemStateAppearance *highlighted = barAppearance.buttonAppearance.highlighted;
        if (highlighted) {
            highlighted.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor darkGrayColor]};
        }
        
        appearance.standardAppearance = barAppearance;
        
    } else {
        // 设置文字属性
        NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
        textAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
        // UITextAttributeFont  --> NSFontAttributeName(iOS7)
        textAttrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:18];            [appearance setTitleTextAttributes:textAttrs];
        
        //设置导航栏的颜色
        [appearance setBarTintColor:[UIColor colorWithHexString:@"#F4895C"]];
        appearance.translucent = YES;
        
    }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值