Reputation: 2364
I am trying to achieve the following navigation bar with two titles and an image:
Large title variant:
Small title variant:
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
}
}
Upvotes: 1
Views: 1417
Reputation: 2364
Inside the ViewController
in 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
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