Can#39;t change UINavigationBar prompt color(无法更改 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 提示颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法更改 UINavigationBar 提示颜色


- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01