How to change navigationBar height in iOS 11?

18,797

Solution 1

Your code is working fine and it´s nothing wrong with it. If you change the background color of your customNavigationBar you´ll see that you´ll get the navigation bar with the desired height. But it seems like it´s an issue with Xcode 9 to hide the default navigation bar.

Your code with:

Xcode 9 enter image description here

Xcode 8 enter image description here

As you can see in the Xcode 9 image, you have the custom navigation bar but the default one does not hide. Probably a bug in Xcode 9, I did not manage to hide it through the Storyboard either.

This seems to be a bug in Xcode 9, bug reports has been filed to Apple.

Solution 2

This is more of a hack till Apple fixes the bug. I was facing the same issue, so I changed the top constraint of the navigation bar from 0 to 20.

Before: enter image description here

After: enter image description here

In case your UINavigationBar backgroundColor is something other than white, this will leave the status bar with a white color. You can fix this by adding the following in that particular UIViewController.

let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
let statusBarColor = UIColor.red
statusBarView.backgroundColor = statusBarColor
view.addSubview(statusBarView)

Before: enter image description here

After: enter image description here

This seems like a lengthy hack, but still better than going back and compiling using Xcode 8.3.

Solution 3

The only way I could make it work was to delete the current custom Navigation Bar, and apply an embed UINavigationController to the UIViewController. Editor -> Embed In -> Navigation Controller.

On the new created Navigation Controller properties, on the Utilities (right side bar) menu 'Simulated Metrics' the 'Top Bar' attribute must be specified. In my case I needed the value: 'Opaque Navigation Bar'.

I also set the same value of 'Top Bar' on my UIViewController, just to make sure.

By doing that, a new 'Navigation Item' will be at your disposal, and you can re-add your Bar Button Items.

It's the best I could do while we wait for the Xcode 9 update to fix it.

Solution 4

Still haven't found how to change it size in pixels. But this is possible to create double navigation bar size (XCode 10.1):

    self.navigationController?.navigationBar.prefersLargeTitles = true

Result:

enter image description here

Solution 5

In iOS 11 we cannot change the navigation bar height, If you want to increase the height we should go with custom view.

Reference: https://forums.developer.apple.com/thread/88202

Share:
18,797

Related videos on Youtube

Mina
Author by

Mina

I'm interested in Swift programming. I have a lot of ideas in my head. I like to learn new things and discover as much as possible. I believe that there is no limitation in this world and everything is right infront of us, we just have to decide to see.

Updated on June 16, 2022

Comments

  • Mina
    Mina about 2 years

    Apparently changing the navigationBar height faced a new approach in iOS 11. in previous iOS versions it was possible to change the navigationBar height by hiding the default navigationBar and adding a new one with custom frame:

    self.navigationController?.setNavigationBarHidden(true, animated: false)
    let customNavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 64))
    self.view.addSubview(customNavigationBar)
    

    But it seems that it is not working in iOS 11 xCode beta. no matter what the new height is, it will always stay at 44.

    this is what I've got in xCode 9:

    enter image description here

    does anyone know how to solve the problem?

  • Mina
    Mina almost 7 years
    yes, i see the problem only in xCode 9 for iOS11. it also hides the default navigation bar and adds the custom on, but the problem is the height don't change to the new one.
  • Rashwan L
    Rashwan L almost 7 years
    @Mina, it does. The custom navigation bar is the green area you see in the Xcode 9 image.
  • Mina
    Mina almost 7 years
    I've updated my question with an image. I've also added ``` customNavigationBar.backgroundColor = .red self.navigationController?.navigationBar.barTintColor = .purple self.navigationController?.navigationBar.barTintColor = .yellow``` but as you can see the navigation bar color didn't changed to purple or yellow, so I think it hides the default navigation bar. as you can see the title is added to the custom one not to the default one
  • Mina
    Mina almost 7 years
    Now I'm confused, because you are right about that it draw the new navigation bar, but why the title is up there? (I doubt about not hiding the default navigation bar)
  • Rashwan L
    Rashwan L almost 7 years
    @Mina, as I said there is some bug. I could not either change the color of the default navigation bar. Your custom navigation bar is the red one and it´s drawn as you can see. Try to file a bug to Apple regarding this.
  • Mina
    Mina almost 7 years
    Thanks for your help, at least I know it draw the custom one.
  • Sulthan
    Sulthan almost 7 years
    Incidentally you can also give a custom UINavigationBar class to your UINavigationController initializer which is easier than hiding a bar and showing a different one.
  • Mina
    Mina almost 7 years
    It's an xCode 9 bug. because I've created ipa with xCode 8.3 and installed it on iOS 11 and everything is ok. but there is a problem with xCode 9 compiler. if i create spa with xCode 9 the problem shows up.
  • Faisal
    Faisal almost 7 years
    It's really a Xcode 9 bug then. The navigation bar appears as intended in Xcode 8.3
  • Rashwan L
    Rashwan L almost 7 years
    @Faisal, indeed.
  • Faisal
    Faisal almost 7 years
    I have filed a bug about the issue.
  • hla
    hla almost 7 years
    So technically the only solution is to wait for a patch on xcode 9 ? What happened if there is no correction ?
  • Rashwan L
    Rashwan L almost 7 years
    @hla, there will probably be some. I’ll see if I can come up with some workaround too.
  • Mina
    Mina almost 7 years
    I've reported the issue too. the bug has still remained in xCode Golden master version.
  • Nevin Jethmalani
    Nevin Jethmalani almost 7 years
    Do you know that this is an issue for sure? Does anybody know a workaround that will allow us to use a navigation bar when we present a view modally?
  • SoftDesigner
    SoftDesigner almost 7 years
    The Simulated Metrics work only in the IDE not in the built app, don't they?
  • Marco Nascimento
    Marco Nascimento almost 7 years
    Exactly, just to have an idea of what will be displaying.
  • Rashwan L
    Rashwan L almost 7 years
    @NevinJethmalani, I have not found any solution yet. This should hopefully be fixed soon.
  • Nayan
    Nayan over 6 years
    Yes. I also have the same issue. My app works fine when run it using xcode 8 but using xcode 9 it shows the gap under navigation bar. Seems like a bug.
  • Mina
    Mina over 6 years
    It increases the possibility of being rejected by apple.
  • Ameya Vichare
    Ameya Vichare over 6 years
    Apple will only reject apps that make use of private APIs which are not recommended for public usage. Having said that, you could adopt this style until Apple fixes the issue.
  • D. Greg
    D. Greg over 6 years
    This is a solid solution until Apple decides to fix the bug. Thanks for posting!
  • Suresh Durishetti
    Suresh Durishetti over 6 years
    @All, please share reported bug link for my reference.
  • Sean Lintern
    Sean Lintern over 5 years
    Any chance you could add the code for updating the top constraint of the navigation bar please? Thanks.