Unable to change hint color of UINavigationBar

I cannot change the color of the invitation in the navigation bar. I tried the code below in viewDidLoadbut nothing happens.

self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]

image on the navigation bar

Am I missing something? Is the code incorrect?

+14
source share
9 answers

You seem to be right about that. To style the invitation text on iOS 11 you need to use UIAppearance.

I filed radar # 34758558 that the property titleTextAttributesjust stopped working for the invitation in iOS 11.

, , Xcode:

UINavigation  iOS 11

// 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:, , .

, , UILabel . , UIAppearance...

UINavigationBar

// 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. ( ..) , :

+10

white iOS 11, barStyle . (, ) - :

myNavbar.barStyle = UIBarStyleBlack; // Objective-C
myNavbar.barStyle = .black // Swift
+19

for view in self.navigationController?.navigationBar.subviews ?? [] {
    let subviews = view.subviews
    if subviews.count > 0, let label = subviews[0] as? UILabel {
        label.textColor = UIColor.white
        label.backgroundColor = UIColor.red
    }
}

,

+2

iOS

    func updatePromptUI(for state: State) {
    if (state != .Online) {
        //workaround for SOFT-7019 (iOS 11 bug - Offline message has transparent background)
        if #available(iOS 11.0, *) {
            showPromptView()
        } else {
            showOldPromptView()
        }
    }
    else {
        self.navigationItem.prompt = nil
        if #available(iOS 11.0, *) {
            self.removePromptView()
        } else {
            self.navigationController?.navigationBar.titleTextAttributes = nil
            self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.lightGray]
        }
    }
}

private func showOldPromptView() {
    self.navigationItem.prompt = "Network Offline. Some actions may be unavailable."
    let navbarFont = UIFont.systemFont(ofSize: 16)
    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: navbarFont, NSAttributedStringKey.foregroundColor:UIColor.white]
}

private func showPromptView() {
    self.navigationItem.prompt = String()
    self.removePromptView()

    let promptView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 18))
    promptView.backgroundColor = .red
    let promptLabel = UILabel(frame: CGRect(x: 0, y: 2, width: promptView.frame.width, height: 14))
    promptLabel.text = "Network Offline. Some actions may be unavailable."
    promptLabel.textColor = .white
    promptLabel.textAlignment = .center
    promptLabel.font = promptLabel.font.withSize(13)
    promptView.addSubview(promptLabel)
    self.navigationController?.navigationBar.addSubview(promptView)
}

private func removePromptView() {
    for view in self.navigationController?.navigationBar.subviews ?? [] {
        view.removeFromSuperview()
    }
}
+2

: →

navController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.red]
0

, , , . , . , UILabels, .

UILabel.appearance(whenContainedInInstancesOf: [NavigationBar.self]).textColor = UIColor.white
0

iOS 11. viewDidLoad navigationItem.prompt = UINavigationController.fakeUniqueText

    navigationController?.promptLabel(completion: { label in
    label?.textColor = .white
    label?.font = Font.regularFont(size: .p12)
        })

    extension UINavigationController {
    public static let fakeUniqueText = "\n\n\n\n\n"
    func promptLabel(completion: @escaping (UILabel?) -> Void) {
        gloabalThread(after: 0.5) { [weak self] in
            guard let 'self' = self else {
                return
            }
            let label = self.findPromptLabel(at: self.navigationBar)
            mainThread {
                completion(label)
            }
        }
    }

    func findPromptLabel(at view: UIView) -> UILabel? {
        if let label = view as? UILabel {
            if label.text == UINavigationController.fakeUniqueText {
                return label
            }
        }
        var label: UILabel?
        view.subviews.forEach { subview in
            if let promptLabel = findPromptLabel(at: subview) {
                label = promptLabel
            }
        }
        return label
    }
    }


    public func mainThread(_ completion: @escaping SimpleCompletion) {
        DispatchQueue.main.async(execute: completion)
    }


    public func gloabalThread(after: Double, completion: @escaping SimpleCompletion) {
       DispatchQueue.global().asyncAfter(deadline: .now() + after) {
       completion()
    }
}

0

UINavigationBar layoutSubviews:

- (void)layoutSubviews {
    [super layoutSubviews];

    if (self.topItem.prompt) {
        UILabel *promptLabel = [[self recursiveSubviewsOfKind:UILabel.class] selectFirstObjectUsingBlock:^BOOL(UILabel *label) {
            return [label.text isEqualToString:self.topItem.prompt];
        }];
        promptLabel.textColor = self.tintColor;
    }
}

, UILabel , . textColor tintColor ( ). , _UINavigationBarModernPromptView . , .

Swift recursiveSubviewsOfKind: selectFirstObjectUsingBlock: 😉.

0
source

You can try this:

import UIKit
class ViewController: UITableViewController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        updatePrompt()
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        updatePrompt()
    }
    func updatePrompt() {
        navigationItem.prompt = " "
        for view in navigationController?.navigationBar.subviews ?? [] where NSStringFromClass(view.classForCoder) == "_UINavigationBarModernPromptView" {
            if let prompt = view.subviews.first as? UILabel {
                prompt.text = "Hello Red Prompt"
                prompt.textColor = .red
            }
        }
        navigationItem.title = "This is the title (Another color)"
    }
}

Screenshot

0
source

All Articles