36

I'm pretty sure this is not duplicate because I looked at other answers and they all outdated and do not provide exact answer.

I have Navigation Controller and several view controllers. I want to make Navigation Bar a bit taller so it would fit text size that I need. How can I do that ?

I tried this:

UINavigationBar.appearance().frame = CGRect(x: 0.0, y: 0.0, width: 320.0, height: 210.0) 

but nothing happens...

Also, I wasn't able to add any constraints to the Nav Bar using Xcode layout buttons.

Hope somebody can help me to fix this issue. Thanks in advance!

enter image description here

Angel F Syrus
  • 1,984
  • 8
  • 23
  • 43
KirillC
  • 780
  • 2
  • 8
  • 21
  • http://stackoverflow.com/questions/40316352/increase-navigationbar-height/40320225#40320225 – Joe Nov 22 '16 at 21:19
  • > If you are trying to do something else that requires the navigation bar to be resized, that's not supported. openradar.appspot.com/32912789 – dimpiax Aug 02 '18 at 07:21

5 Answers5

39

UPDATE 8/3/20: I posted this in 2016. A number of people have stated this no longer works so please use at your own risk. I am not working in iOS at the moment so I do not have an update handy. Best of luck!

Here is one way to do it:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let height: CGFloat = 50 //whatever height you want to add to the existing height
    let bounds = self.navigationController!.navigationBar.bounds
    self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height)
    
}
Frankie
  • 11,508
  • 5
  • 53
  • 60
  • Great thanks for your answer, worked just fine. To my surprise I added that code in one VC and it worked for the entire app... I tried to change your code a bit and enter it to App Delegate but it didn't work unfortunately. But I guess that's fine. – KirillC Nov 22 '16 at 21:22
  • 2
    @KirillC You could subclass UINavigationController and add Frankie's code there. – Brett Ponder Nov 22 '16 at 21:49
  • @KirillC You're welcome. Please consider accepting the answer if it helped. You could do as Brett said. Also if you only need it to work in one VC you could add a similar method to viewWillDisappear to change the height back. – Frankie Nov 22 '16 at 23:00
  • I just added the code to my initial VC and it works fine on all other VCs, why should I subclass? – KirillC Nov 23 '16 at 16:16
  • I think he was suggesting that you 'could' subclass, not necessarily 'should' subclass. Depends on your situation. Your initial VC probably has the same navigation controller as your other vc's, therefore it suffices. If you had multiple navigation controllers throughout your app, it would make sense to subclass as opposed to rewriting the same code in each one. If my answer helped you I'd appreciate if you accepted the answer, thanks! – Frankie Nov 23 '16 at 16:24
  • Subclassing doesn't work with the above code, I'm getting an error `unexpected found nil`. See picture: http://imgur.com/a/Oyuy3 – MarksCode Jun 02 '17 at 20:09
  • @MarksCode If you’re in a `UINavigationController` subclass probably need to change any references of `self.navigationController` to just `self`. – Frankie Jun 05 '17 at 12:57
  • @Frankie this works for me.. but I am facing another issue here. If I click device home button and open the app again, the navigation bar jumps to top and there is a gap between my view nd navigationbar. Any idea? Any help is appreciated. – QUserS Aug 16 '17 at 07:16
  • @QUserS I'm not certain if it's related or not. I'd recommend posting another question with detailed code, images, etc, and see if someone can help you. – Frankie Aug 16 '17 at 17:21
  • 40
    I tried it and it simply does not work. Maybe it's the new iOS version, but there's no effect after calling this code. – b005t3r Jul 09 '18 at 09:51
  • 4
    > If you are trying to do something else that requires the navigation bar to be resized, that's not supported. https://openradar.appspot.com/32912789 – dimpiax Aug 02 '18 at 07:20
12

You can write this code in class that extends UINavigationController

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    let height = CGFloat(72)
    navigationBar.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: height)
}
kbokdia
  • 439
  • 5
  • 11
8

You can create a class based on UINavigationBar like this

class TTNavigationBar: UINavigationBar {

    override func sizeThatFits(_ size: CGSize) -> CGSize {
        return CGSize(width: UIScreen.main.bounds.width, height: 55)
    }

}

And add it to your NavigationController.

Rav
  • 1,327
  • 3
  • 18
  • 32
Patricio Bravo
  • 406
  • 6
  • 8
  • 4
    Do you have any idea why this method never gets called in my case? I basically just copy-pasted this code to my custom UINavigationBar class and set it in storyboard on my navigation bars. – andrejbroncek Jul 05 '17 at 15:50
  • @andrejbroncek are you sure your custom UINavigationBar it's executing? -One common problem is that sometimes you end up with several UINavigationController and maybe your custom UINavigationBar is never executed or is not the one that is displayed – Patricio Bravo Jul 07 '17 at 18:36
  • same problem, does not get executed – WalterBeiter Nov 27 '19 at 08:57
  • 1
    for some reasons iOS 13 doesn't call this method – Konstantin Nikolskii Sep 30 '20 at 13:12
4

This might be better if your navigation bars in your app all have same adjusted height.

extension UINavigationBar {
    open override func sizeThatFits(_ size: CGSize) -> CGSize {
        return CGSize(width: UIScreen.main.bounds.width, height: 51)
    }
}
dimpiax
  • 12,093
  • 5
  • 62
  • 45
HMHero
  • 2,333
  • 19
  • 11
-3

you can draw your navigation bar from xib (with your height) and then:

self.navigationController?.navigationBar.addSubview(yourNavBarView)

in alternative, if you want create UIView from code,

let viewNavBar = UIView(frame: CGRect(
    origin: CGPoint(x: 0, y:0), 
    size: CGSize(width: self.view.frame.size.width, height: 100)))

and then adding to nav bar.

matt
  • 515,959
  • 87
  • 875
  • 1,141
Alessio Campanelli
  • 970
  • 11
  • 19
  • 1
    You do not need to say `init` like that. — A major flaw here is that you have created a vanilla UIView, not a UINavigationBar which is what the OP wants. – matt Nov 22 '16 at 21:33