iosswiftuinavigationbarlarge-titlepreferslargetitles

custom navigation bar with image and two titles in iOS


I am trying to achieve the following navigation bar with two titles and an image:

Large title variant:

large

Small title variant:

small

I tried subclassing UINavigationBar and adding subviews to it, but they did not render at all. I tried setting a titleView in storyboard, however it seemed like the titleView is constrained in its height.

What is the proper way to achieve this custom navigation bar?

I also tried this (and setting the viewController in Storyboard to that class):

class NavViewController: UINavigationController {

    var titleView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationBar.topItem?.titleView?.backgroundColor = .gray
        titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 300)
        self.navigationBar.topItem?.titleView = titleView
    }
}

Storyboard


Solution

  • Inside the ViewControllerin viewDidLoad, add self.navigationController?.navigationBar.addSubview(imageView). (no need for subclassing) There is even support for AutoLayout inside UINavigationbar, which is great for animation.