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
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01