AustinT
AustinT

Reputation: 2026

Custom navigationItem.titleView not setting

I am having troubles setting the self.navigationItem.titleView, could someone please help me catch my mistake.

import Foundation

class CustomNavigationController: UINavigationController
{
    override func viewDidLoad() {
        let logo = UIImage(named: "browse_back")
        var hexColor = 0x21BBD4 as UInt
        self.navigationBar.barTintColor = GeneralHelper.UIColorFromRGB(hexColor)
        self.navigationItem.titleView = UIImageView(image: logo)
    }
}

Here is my code for setting the titleView to an image.

enter image description here enter image description here

When I run the application, the color of the navigation bar is being changed to the correct color, but the titleView image is not displaying.

Thanks.

Upvotes: 1

Views: 3783

Answers (2)

Woody Jean-louis
Woody Jean-louis

Reputation: 535

For those using a UILabel as your titleView

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    navigationItem.titleView?.sizeToFit()
}

Hope this works!

Upvotes: 6

Rafał Sroka
Rafał Sroka

Reputation: 40028

The managing UINavigationController object uses the navigation items of the topmost two view controllers to populate the navigation bar with content.

Source: UINavigationItem Class Reference

You have to set the titleView of the navigationItem of the controller that is the top most controller in the navigation stack managed by your custom navigation controller.

Upvotes: 8

Related Questions