无法更改 UINavigationBar 提示颜色 [英] Can't change UINavigationBar prompt color

查看:21
本文介绍了无法更改 UINavigationBar 提示颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法更改导航栏上的提示颜色.我在 viewDidLoad 中尝试了下面的代码,但没有任何反应.

I am unable to change the prompt color on my navigation bar. I've tried the code below in viewDidLoad, but nothing happens.

self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]

我错过了什么吗?上面的代码错了吗?

Am I missing something? Is the code above wrong?

推荐答案

看来你是对的.您需要使用 UIAppearance 在 iOS 11 上设置提示文本样式.

It seems like you're right about this one. You need to use UIAppearance to style the prompt text on iOS 11.

我已经提交了雷达 #34758558,即 titleTextAttributes 属性刚刚停止在 iOS 11 中提示提示.

I've filed radar #34758558 that the titleTextAttributes property just stopped working for prompt in iOS 11.

好消息是有几个变通方法,我们可以通过使用 Xcode 的视图层次调试器来发现:

The good news is that there are a couple of workarounds, which we can uncover by using Xcode's view hierarchy debugger:

// 1. This works, but potentially changes *all* labels in the navigation bar. 
// If you want this, it works.
UILabel.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).textColor = UIColor.white

提示只是一个UILabel.如果我们使用 UIAppearance 的 whenContainedInInstancesOf:,我们可以很容易地按照我们想要的方式更新颜色.

The prompt is just a UILabel. If we use UIAppearance's whenContainedInInstancesOf:, we can pretty easily update the color the way we want.

如果您仔细观察,您会注意到 UILabel 上还有一个包装器视图.它有自己的类,可能会响应 UIAppearance...

If you look closely, you'll notice that there's also a wrapper view on the UILabel. It has its own class that might respond to UIAppearance...

// 2. This is a more precise workaround but it requires using a private class.

if let promptClass = NSClassFromString("_UINavigationBarModernPromptView") as? UIAppearanceContainer.Type
{
  UILabel.appearance(whenContainedInInstancesOf: [promptClass]).textColor = UIColor.white
}

我建议坚持使用更通用的解决方案,因为它不使用私有 API.(应用审查等)看看这两种解决方案中的任何一种都有什么效果:

I'd advise sticking to the more general solution, since it doesn't use private API. (App review, etc.) Check out what you get with either of these two solutions:

这篇关于无法更改 UINavigationBar 提示颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆