Lachtan
Lachtan

Reputation: 5172

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

Upvotes: 1

Views: 302

Answers (1)

Najdan Tomić
Najdan Tomić

Reputation: 2111

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.

Upvotes: 1

Related Questions