40

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
  • 34,719
  • 24
  • 185
  • 251
Mayur Tolani
  • 1,268
  • 2
  • 16
  • 26

11 Answers11

43

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
  • 80,161
  • 21
  • 166
  • 138
  • 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
  • @DanielSpringer - I will check and let you know ASAP – Anbu.Karthik Jan 04 '19 at 03:42
15

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,360
  • 1
  • 18
  • 31
khushbu Tadvi
  • 151
  • 1
  • 2
  • 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
14

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,362
  • 3
  • 41
  • 56
12

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,542
  • 3
  • 27
  • 37
Meghs Dhameliya
  • 2,362
  • 23
  • 28
11

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
  • 860
  • 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
11

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,140
  • 3
  • 28
  • 43
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,528
  • 3
  • 35
  • 57
Abhishek Thapliyal
  • 2,920
  • 4
  • 25
  • 59
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
  • 418
  • 4
  • 8
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
  • 3,933
  • 2
  • 16
  • 31
0
    navigationController?.additionalSafeAreaInsets.top = 25

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

JESTIN SAJI
  • 25
  • 10
-4

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