iOS13 - 导航栏标题颜色问题

8

我有一个故事板应用程序,它包含多个视图控制器和一个标签栏控制器。目前导航栏标题的颜色是白色的。现在我正在使用 Xcode 11 beta 6 和 iOS 13 beta 8 进行测试,发现标题变成了黑色。在安装 iOS 12 的设备上,标题仍然是白色的。 我尝试在故事板中设置导航控制器的导航栏标题颜色,但这没有作出任何差别。 我还尝试在每个视图中更改标题颜色,但有时候无效。 在开始使用 iOS 13 进行测试时,我必须更改代码以更改状态栏的背景颜色。代码如下:

self.tabBarController.title = NSLocalizedString(@"AppTitle",nil);

NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor clearColor];
shadow.shadowOffset = CGSizeMake(0, 1);

[self.navigationController.navigationBar setBarTintColor:COLOR_HEADER_LIGHT];

if (@available(iOS 13, *))
{
    UINavigationBarAppearance *navBar = [[UINavigationBarAppearance alloc] init];
    navBar.backgroundColor = COLOR_HEADER_LIGHT;
    self.navigationController.navigationBar.standardAppearance = navBar;
    self.navigationController.navigationBar.scrollEdgeAppearance = navBar;
}
else
{
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = COLOR_HEADER_LIGHT;
    }
}

[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                 shadow, NSShadowAttributeName, FONT_MEDIUM_SIZE_18, NSFontAttributeName,
                                                                 COLOR_TEXT_WHITE, NSForegroundColorAttributeName, nil]];

[self.navigationController.navigationBar setTintColor:COLOR_TEXT_WHITE];

我希望任何人都能想出如何将标题颜色改回白色。最好的情况是不必调整每个控制器。

COLOR_TEXT_WHITE 是如何定义的? - LorenzOliveto
#define COLOR_TEXT_WHITE [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0] 的意思是定义一个白色文本颜色的常量,它使用了RGB颜色空间,具体数值为红255、绿255、蓝255,不透明度为1.0。 - Henning
4个回答

10
func manageNavigationBar(){
        if #available(iOS 13.0, *){

            let navBarAppearance = UINavigationBarAppearance()
            navBarAppearance.configureWithOpaqueBackground()
            navBarAppearance.backgroundColor = UIColor(red: 0.6157, green: 0.3412, blue: 0.8588, alpha: 1.0)
            navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
            navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
            UINavigationBar.appearance(whenContainedInInstancesOf: [UINavigationController.self]).standardAppearance = navBarAppearance
            UINavigationBar.appearance(whenContainedInInstancesOf: [UINavigationController.self]).scrollEdgeAppearance = navBarAppearance
        }
    }

//在你的AppDelegate中调用此函数,以及你想要导航工作方式的类中也调用此函数


2
这个问题的最佳答案。谢谢。 - mgyky

4
在iOS 13中,我可以这样更改标题颜色:
UINavigationBarAppearance *appearance = [self.navigationController.navigationBar standardAppearance];
appearance.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
self.navigationController.navigationBar.standardAppearance = appearance;
self.navigationController.navigationBar.scrollEdgeAppearance = appearance;

1
在iOS 13中,您需要在UINavigationBarAppearance对象上设置标题颜色-尝试将此行添加到您的代码中:
appearance.titleTextAttributes = [.foregroundColor: myAppLabelColor]
appearance.largeTitleTextAttributes = [.foregroundColor: myAppLabelColor]

0

iOS 13使用动态颜色来支持浅色和深色主题,您可以通过创建浅色和深色外观的颜色资源来设置标题颜色。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接