当“prefersLargeTitles”时,更改导航栏标题的文本颜色。设置为true [英] Changing the text color of a navigation bar title when "prefersLargeTitles" is set to true

查看:407
本文介绍了当“prefersLargeTitles”时,更改导航栏标题的文本颜色。设置为true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,我必须使用带有红色大标题的 UINavigationBar



目前,我有以下代码:

  func prepareNavigationController(){
let navController = UINavigationController(rootViewController:self)
navController.navigationBar.prefersLargeTitles = true
navigationItem.searchController = UISearchController(searchResultsController:nil)
navigationItem.hidesSearchBarWhenScrolling = false
navController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.red]
}

但实际上并没有将标题标签染成红色。结果如下:





但更改 prefersLargeTitles to false做正确的事,我的标题是红色。



navController.navigationBar.prefersLargeTitles = false





我是不完全确定这是否是一个错误,因为在撰写本文时我们仍然处于第一个测试阶段,或者如果这是故意的行为,主要是因为我之前没有任何Apple的应用程序为大型游戏着色。有没有办法真正让大标题有我想要的任何颜色?

解决方案

有一个新的UINavigationBar属性largeTitleTextAttribute 这应该有助于此。





以下是没有设置largeTitleTextAttributes的示例代码和屏幕截图,但barStyle设置为.black

  navigationC ontroller?.navigationBar.barStyle = .black 



这是没有设置largeTitleTextAttributes的屏幕截图,但barStyle设置为.default

  navigationController?.navigationBar.barStyle = .default 


I have a requirement in which I have to use a UINavigationBar with a red large title.

Currently, I have the following code:

func prepareNavigationController() {
    let navController = UINavigationController(rootViewController: self)
    navController.navigationBar.prefersLargeTitles = true
    navigationItem.searchController = UISearchController(searchResultsController: nil)
    navigationItem.hidesSearchBarWhenScrolling = false
    navController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.red]
}

But it's not actually tinting the title label to red. This is the result:

But changing prefersLargeTitles to false does the right thing, and my title is red.

navController.navigationBar.prefersLargeTitles = false

I am not entirely sure if this is a bug since at the time of this writing we are still in the first beta, or if this is intentional behavior, mostly because I haven't any of Apple's apps color the large titles before. Is there any way to actually get the large title to have any color I want?

解决方案

There is a new UINavigationBar property "largeTitleTextAttribute" that should help with this.

largeTitleTextAttribute

Here is a sample code you can add to your view controllers viewDidLoad method

        navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue]

Here is a sample code and screenshot without the largeTitleTextAttributes set, but the barStyle is set to .black

        navigationController?.navigationBar.barStyle = .black

Here is a screenshot without the largeTitleTextAttributes set, but the barStyle is set to .default

        navigationController?.navigationBar.barStyle = .default

这篇关于当“prefersLargeTitles”时,更改导航栏标题的文本颜色。设置为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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