26

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
  • 2,167
  • 2
  • 25
  • 32

6 Answers6

12

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.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • 1
    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. – Mina Sep 10 '17 at 07:43
  • @Mina, it does. The custom navigation bar is the green area you see in the Xcode 9 image. – Rashwan L Sep 10 '17 at 07:45
  • 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 Sep 10 '17 at 08:07
  • 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) – Mina Sep 10 '17 at 08:08
  • @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. – Rashwan L Sep 10 '17 at 08:10
  • 2
    Thanks for your help, at least I know it draw the custom one. – Mina Sep 10 '17 at 08:11
  • 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. – Sulthan Sep 10 '17 at 08:22
  • 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. – Mina Sep 10 '17 at 10:52
  • It's really a Xcode 9 bug then. The navigation bar appears as intended in Xcode 8.3 – Faisal Sep 21 '17 at 10:32
  • @Faisal, indeed. – Rashwan L Sep 21 '17 at 10:40
  • 2
    I have filed a bug about the issue. – Faisal Sep 21 '17 at 11:07
  • So technically the only solution is to wait for a patch on xcode 9 ? What happened if there is no correction ? – hla Sep 21 '17 at 14:55
  • @hla, there will probably be some. I’ll see if I can come up with some workaround too. – Rashwan L Sep 21 '17 at 14:58
  • 2
    I've reported the issue too. the bug has still remained in xCode Golden master version. – Mina Sep 22 '17 at 07:12
  • 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? – Nevin Jethmalani Sep 24 '17 at 16:22
  • @NevinJethmalani, I have not found any solution yet. This should hopefully be fixed soon. – Rashwan L Sep 25 '17 at 18:25
  • 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. – Nayan Oct 04 '17 at 12:10
  • @All, please share reported bug link for my reference. – Suresh Durishetti Oct 31 '17 at 06:48
12

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.

Ameya Vichare
  • 1,119
  • 1
  • 10
  • 22
  • It increases the possibility of being rejected by apple. – Mina Oct 09 '17 at 09:38
  • 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. – Ameya Vichare Oct 09 '17 at 09:47
  • 1
    This is a solid solution until Apple decides to fix the bug. Thanks for posting! – D. Greg Oct 13 '17 at 00:39
  • Any chance you could add the code for updating the top constraint of the navigation bar please? Thanks. – Sean Lintern Mar 06 '19 at 14:22
2

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.

Marco Nascimento
  • 832
  • 8
  • 15
1

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

Bharath
  • 2,064
  • 1
  • 14
  • 39
1

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

Alex
  • 751
  • 1
  • 9
  • 28
0

This answer did the job for me.

navigationController.navigationBar.setTitleVerticalPositionAdjustment(CGFloat(10),
forBarMetrics: UIBarMetrics.Default)
Declan McKenna
  • 4,321
  • 6
  • 54
  • 72