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

前端 未结 5 568
一向
一向 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:22

    The way you do this in iOS 13 has changed, you now use UINavigationBarAppearance class like this…

    let appearance = UINavigationBarAppearance(idiom: .phone)
    appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.systemRed]
    appearance.titleTextAttributes = [.foregroundColor: UIColor.systemRed]
    appearance.backgroundColor = .white
    navigationItem.standardAppearance = appearance
    navigationItem.scrollEdgeAppearance = appearance
    
    0 讨论(0)
  • 2020-12-28 14:27

    Not sure if it's a bug in beta 1 & 2, but here is a way to set the color. It's a bit of a "hacky" workaround, but it should work until Apple fixes this. In both the Objective-C and Swift version, this code goes in the viewDidAppear: method.

    Objective-C:

    dispatch_async(dispatch_get_main_queue(), ^{
        for (UIView *view in self.navigationController.navigationBar.subviews) {
            NSArray <__kindof UIView *> *subviews = view.subviews;
            if (subviews.count > 0) {
                UILabel *label = subviews[0];
                if (label.class == [UILabel class]) {
                    [label setTextColor:[UIColor redColor]];
                }
            }
        }
    });
    

    Swift:

    DispatchQueue.main.async {
         for view in self.navigationController?.navigationBar.subviews ?? [] {  
         let subviews = view.subviews  
         if subviews.count > 0, let label = subviews[0] as? UILabel {  
               label.textColor = UIColor.red
     } } }
    
    0 讨论(0)
  • 2020-12-28 14:33

    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
    

    0 讨论(0)
  • 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)

    0 讨论(0)
  • 2020-12-28 14:41

    If using storyboard, just change "Large Title Text Attributes" Title Color at Navigation Bar Attribute Inspector:

    0 讨论(0)
提交回复
热议问题