WalterBeiter
WalterBeiter

Reputation: 2364

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

Upvotes: 1

Views: 1417

Answers (2)

WalterBeiter
WalterBeiter

Reputation: 2364

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.

Upvotes: 2

Zeeshan Ahmed
Zeeshan Ahmed

Reputation: 864

Design your custom view seprately in a xib file, then set that xib as the titleview for your navigationbar

self.navBar.topItem?.titleView = logoImage

Do this for large title, for the smaller one only populate an image in the titleView.

Upvotes: 0

Related Questions