IOS 改变导航栏颜色
2020-11-11 本文已影响0人
奇梦人
- 替换系统自带的返回箭头
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];
}
- 更换导航栏颜色
这里需要做兼容,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;
}