Search code examples
swiftuinavigationcontroller

Custom UINavigationController: title color


I need to make a custom navigation controller class for my project, but I'm struggling with navigation bar title color. Bar tint color and tint color are changing properly, but I can't change color of title. Here's my code:

    class SANavigationController: UINavigationController {

    // MARK: - Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationBar.isTranslucent = false
        setupAppearance()
        setupBehaviour()
    }

    // MARK: - Setup

    func setupAppearance() {
        navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue]
        navigationBar.tintColor = .red
        navigationBar.barTintColor = .blue
    }

    private func setupBehaviour() {
        if #available(iOS 11.0, *) {
            navigationBar.prefersLargeTitles = true
            navigationItem.largeTitleDisplayMode = .automatic
        }
    }
}

Thanks in advance


Solution

  • To change text color of large title you should use

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

    Although it's a bit strange to have same color for text color and barTintColor it won't be visible.