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

查看:169
本文介绍了无法更改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天全站免登陆