Changing the text color of a navigation bar title when “prefersLargeTitles” is set to true

前端 未结 5 586
一向
一向 2020-12-28 14:12

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

Currently, I have the following code:

func prepare         


        
5条回答
  •  情话喂你
    2020-12-28 14:38

    Here's the working code to use large titles and sets the text color of small and large titles to white, both on iOS11+ and on older iOS versions.

    // Will apply to versions before iOS 11
    navigationController?.navigationBar.titleTextAttributes = [
        NSAttributedStringKey.foregroundColor: UIColor.white
    ]
    
    if #available(iOS 11.0, *) {
        navigationController?.navigationBar.prefersLargeTitles = true
        navigationController?.navigationBar.largeTitleTextAttributes = [
            NSAttributedStringKey.foregroundColor: UIColor.white
        ]
    }
    

    (There used to be a bug in Xcode, but it now appears to be fixed)

提交回复
热议问题