首页 > 解决方案 > 在 Xcode 13 [[UINavigationBar 外观] setBarTintColor: 无法正常工作?

问题描述

我已将我的 Xcode 更新为 13,后来在我的旧项目导航和标签栏颜色中的文字更改为透明。

我的代码是

[[UINavigationBar appearance] setBarTintColor:[UIColor AppThemeColour]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTranslucent:NO];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

我尝试添加背景颜色,但导航栏的标题和图像没有出现。

self.navigationController.navigationBar.backgroundColor = [UIColor bOneAppThemeColor];
[[UINavigationBar appearance] setBarTintColor:[UIColor AppThemeColour]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTranslucent:NO];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

我已经研究了下面的链接,但我无法在 Objective C 中实现它

https://developer.apple.com/forums/thread/682420

标签: iosobjective-cxcodeuinavigationcontrolleruinavigationbar

解决方案


您所做的几乎所有事情都是错误的(并且已经错了好几年)。您需要使用 UINavigationBarAppearance (和 UITabBarAppearance)并将它们应用于 barstandardAppearance和它的scrollEdgeAppearance. 设置外观的背景颜色,而不是它的色调颜色。并且永远不要触摸该translucent物业。

在这个简单的示例中,我们让所有导航栏都采用您的主题颜色。修改以满足您的需求和愿望:

if (@available(iOS 13.0, *)) {
    UINavigationBarAppearance* appear = [UINavigationBarAppearance new];
    appear.backgroundColor = [UIColor AppThemeColor];
    id proxy = [UINavigationBar appearance];
    [proxy setStandardAppearance: appear];
    [proxy setScrollEdgeAppearance: appear];
} else {
    // Fallback on earlier versions
}

推荐阅读