42

I'm working on a app, in which I need to keep a navigation bar. when I write any title on the bar, the time and the title kinda get very close to each other. I wanted to increase the height of the bar, so it can get some breathing room.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mayur Tolani
  • 1,458
  • 3
  • 17
  • 28

11 Answers11

45

select your ViewController --> select your Navigation Item --> Prompt --> Add space it increase the height of **Navigation bar**

Check Image here : enter image description here

Programatically

Add this in viewWillAppear or viewDidAppear method

Objective-C

[self.navigationController.navigationBar setFrame:CGRectMake(0, 0, self.view.frame.size.width,80.0)];

Swift

self.navigationController.navigationBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 80.0)

Swift-3

self.navigationController!.navigationBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 80.0)

iOS 11

enter image description here

objective C

for (UIView *subview in self.navigationController.navigationBar.subviews) {
    if ([NSStringFromClass([subview class]) containsString:@"BarBackground"]) {
        CGRect subViewFrame = subview.frame;
        // subViewFrame.origin.y = -20;
        subViewFrame.size.height = 100;
        [subview setFrame: subViewFrame];
    }
}

swift

for subview in (self.navigationController?.navigationBar.subviews)! {
       if NSStringFromClass(subview.classForCoder).contains("BarBackground") {
            var subViewFrame: CGRect = subview.frame
            // subViewFrame.origin.y = -20;
            subViewFrame.size.height = 100
            subview.frame = subViewFrame

        }

    }
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • The moving the prompt with white text WORKS , not sure why but okay :D – Kingsley Mitchell Jun 05 '17 at 06:16
  • Setting programatically in viewWillAppear does not work in iOS 10 – Julius Aug 25 '17 at 22:06
  • 1
    @Anbu.Karthik, bro your answer works for me but for collectionView Controller, the navigation bar position is back and Collection VC scroll View is front. How can I set navigation bar to front ? – May Phyu Sep 23 '17 at 04:24
  • @adev - check the updated answer, its working fine for me in iOS 11 – Anbu.Karthik Sep 29 '17 at 14:23
  • @Anbu.Karthik, I meant the programmatically setting thing. I dont want to set the prompt since I cant adjust left and right barbutton items to keep it in middle. It looks weird without ability to adjust those buttons. I wanted to set it in code. – adev Sep 29 '17 at 21:06
  • Still same result for me. Can you please try with left and right bar button items added too. Was it moving down when you do this? – adev Sep 30 '17 at 20:52
  • 1
    is it also possible to animate the "growth" of the navbar? The transition to the bigger navbar is not smooth from one VC (with a small one) to the next – RaptoX Jan 19 '18 at 11:52
  • Works for me in iOS 11 as well as in iOS 10. Thanks for the answer – Atpl Mohali Mar 09 '18 at 05:04
18

simply add this line to your viewController

navigationController?.additionalSafeAreaInsets.top = 30 
  // where 30 is the extra space, add as per your need. 
Mohit Kumar
  • 2,898
  • 3
  • 21
  • 34
khushbu Tadvi
  • 189
  • 1
  • 2
  • 1
    In my case, I have to hide my statusBar. So I did that by overriding view controllers method. It created a problem. When I was hiding status bar, my navbar was moving up and down when statusbar was unhidden. So I need to add space in place of statusbar. And this above solution worked for me. – Amit Kumar Sep 04 '21 at 14:23
15

Apple proposes not to resize navigationBar itself, but remove shadow from bar and add custom view under your navigationBar. This can work for most cases. Check Apple's samples.

Timur Bernikovich
  • 5,660
  • 4
  • 45
  • 58
12

Add the following extension to your project:

import UIKit

extension UINavigationBar {

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

}
odm
  • 888
  • 1
  • 14
  • 22
  • How to call this extension? – Hemang Aug 24 '17 at 05:15
  • 1
    @Hemang this method is automatically called when the UINavigationBars in your app are drawn. There's no need to call it by yourself. – odm Aug 24 '17 at 13:41
  • 18
    This doesnt work in iOS 11. Even though it is getting called, nav bar still has old height. – adev Sep 29 '17 at 06:57
12

Please refer the apple recommended approach for extended navigation bar here,

https://developer.apple.com/library/content/samplecode/NavBar/Introduction/Intro.html

Naveen Shan
  • 9,192
  • 3
  • 29
  • 43
  • 13
    Link only answers can become useless if the link breaks. Please provide a minimal set of instructions in the post itself. –  Jan 03 '19 at 22:45
  • this link is Apple long term link and the best answer described here. Other answers are more hacks than convenient way to use nav bar and nav bar ctrl – Nikola Jovic Jan 29 '19 at 11:00
  • ExtendedNavBar is not part of the sample Xcode project – twodayslate Jun 14 '19 at 14:15
  • 5
    The sample code pointed to by that link doesn't include an example of increasing the height. – John Scalo Feb 07 '20 at 22:46
11

THIS SOLUTION NO LONGER WORKS IN Xcode 8.x.x and later!

you can also increase height without creating the custom navigation follow the following steps

Step 1 Selecte Navigation bar in Storyboard or XIB

enter image description here

Step 2 Copy ObjectID from Identity Inspector

enter image description here

Step 3 Open Storyboard/XIB as Source Code

enter image description here

Step 4 Find ObjectID in Source Code past ObjectID in search

enter image description here

Step 5 Edit height! thats all

enter image description here

I hope this will help you

Islam
  • 3,654
  • 3
  • 30
  • 40
Meghs Dhameliya
  • 2,406
  • 24
  • 29
8

Add this in viewWillAppear method

CGFloat height = 80;
[self.navigationController.navigationBar setFrame:CGRectMake(0, 0,
self.view.frame.size.width,height)];

if it increase first and shrinks to original height then add this code in viewDidAppear method

byJeevan
  • 3,728
  • 3
  • 37
  • 60
Abhishek Thapliyal
  • 3,497
  • 6
  • 30
  • 69
1

We need to change the height of the navigation bar for each time the view show.So put the code on viewWillAppear

override func viewWillAppear(_ animated: Bool) {
     self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 80)
}

we can set the width as the width of the view and change the height as we wish.

Sandu
  • 436
  • 4
  • 8
1
    navigationController?.additionalSafeAreaInsets.top = 25

Add this to viewDidLoad. it will definitely work. Successfully worked in Xcode 12-version

JESTIN SAJI
  • 53
  • 1
  • 15
  • This updates the UINavigationController overall area, but the UINavigationBar remains at 44px. – rosem Oct 21 '22 at 16:46
0

You can't change the height of the default NavigationBar if I'm not wrong.

Although, you can create a custom NavigationBar and add a custom height to it.

danialzahid94
  • 4,103
  • 2
  • 19
  • 31
-4

[self.navigationController.navigationBar setPrefersLargeTitles:YES]; is going to Increase the Navigation bar height Programmatically