1

I'm trying to understand how to change the height of a navigation bar. Whenever I insert one into my app, it is shorter than all the navigation bars used in Apple's stock apps (Messages and Settings for example). I would like it to get to that height because when I try to add a bar button, it conflicts with the status bar. I also read that as a developer you shouldn't change the height of the navigation bar so I'm a bit confused. Finally, I looked at this Stack Overflow page: How can I change height of Navigation Bar Swift 3. I tried to implement the code...

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let height: CGFloat = 50 //whatever height you want
    let bounds = self.navigationController!.navigationBar.bounds
    self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height)

}

...but my app crashed when I did so. I'm using Xcode 8.2 beta with Swift 3.

Community
  • 1
  • 1
aequinox
  • 125
  • 1
  • 17
  • Show the code that caused a crash. – toddg Jan 18 '17 at 18:05
  • @toddg override fund viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) let height: CGFloat = 50 let bounds = self.navigationController!.navigationBar.bounds self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height) } – aequinox Jan 18 '17 at 18:49
  • Edit your question and put the code there. – toddg Jan 18 '17 at 18:50
  • I did that now @toddg – aequinox Jan 18 '17 at 18:53
  • It probably crashes on the line with `self.navigationController!.navigationBar.bounds`, right? If that's the case, the crash probably occurs due to the non-existence of a `navigationController`. – xoudini Jan 18 '17 at 18:59
  • Are you sure your navigation bar is embedded in a navigation controller? Change your third line to let bounds = self.navigationController?.navigationBar.bounds – toddg Jan 18 '17 at 19:00
  • it still crashes @toddg – aequinox Jan 18 '17 at 19:03
  • How would I fix that @xoudini I'm pretty new to this... – aequinox Jan 18 '17 at 19:04
  • What error does the console spit out? Use the debugger to figure out what line is causing the crash. – toddg Jan 18 '17 at 19:05
  • Thanks for your help xoudini and toddy -- Aragunz was able to help me. – aequinox Feb 04 '17 at 19:43
  • https://stackoverflow.com/questions/39454548/ios-10-custom-navigation-bar-height/41613047 – Deniss Fedotovs Jul 26 '17 at 07:31

2 Answers2

0

I dont think you can change size of navigationBar. But this is how would i recommend you to do it.

  1. Remove default navigation bar.

  2. Create a view which is similar to a navigation bar like you design add constrains> leading,trailing,top and height for that view. so basicly pin it to top, left and right with your design's height.

  3. Add 2 buttons left and right if needed which would look similar to navigation bar. add button constrains> leading, top, bottom and width for left one and trailing,top, bottom and width right one.
  4. Add a UILabel which would be your navigation bar title., add label constrains leading with left button, trailing with right button, top and bottom with navigation view you created. Make uilabel text centered.

Here you go u have your custom navigation bar. On each controller all u have to do is CMD+C and CMD+V on the other controller add leading,trailing and top constrains. Hope it helped. enter image description here

Aragunz
  • 511
  • 5
  • 18
  • This is definitely helpful (although I'm not sure why Apple doesn't let devs change the nav bar height), but then a second question arrises. How do a get a normal button look like a bar button. I am trying to make the button look like an 'X' which is easy with a bar button (set it to the 'stop' under system item), but not (to my knowledge) with a normal button. – aequinox Jan 19 '17 at 02:56
  • @AEquinox01 All you have to do is get the X like png probably from design and set it as button image, make button as system button,not custom, and in tint color u can change that X color to whatever u need. Let me know if u need more help If this helped you plz accept the answer. – Aragunz Jan 19 '17 at 09:20
  • I can't seem to find the png -- could you explain in a bit more detail how to do that? @Florentt – aequinox Jan 19 '17 at 13:14
  • @AEquinox01 Does the designer provide the X button png image for your app ? – Aragunz Jan 19 '17 at 13:37
  • Maybe I didn't explain well -- I'm trying to get the X button image provided by Apple as an option for bar buttons (entitled 'stop' under system item), but I don't know how to because I cannot use a bar button because I am using a view instead of a navigation bar. Does that make more sense? @Florentt – aequinox Jan 19 '17 at 14:26
  • @AEquinox01 i understand, yes you can't do that with UIbutton. But you can download the X button for free on web, Here is an example of X, download it as PNG, >>> http://www.flaticon.com/free-icon/close-button_61155 – Aragunz Jan 19 '17 at 14:46
  • If this is helpful please upvote my effort, thank you. – Aragunz Jan 19 '17 at 14:48
  • 1
    Thanks Florentt -- I did upvote but I have too few reputation for it to show up, sorry... – aequinox Jan 19 '17 at 15:55
0

It is possible, and simple, to add an independent navigation bar and have it match the normal navigation bar height and rotation functionality. Here is how to do it (link includes a video):

In Interface Builder:

  • Add a UINavigationBar to your view, positioned at the Top Layout Guide location.

  • Set constraints for Leading Space to Container Margin, Trailing Space to Container Margin, and Top Space to Container — all with ‘Relative to margin’ deselected and a Constant value of 0 (zero).

  • With the Navigation Bar selected, in the Identity Inspector, add a key path called barPosition. Give it a Number type, and a value of 3.

That should be all you need. However, if you’ve completed these steps and your project doesn’t seem to like the key path, then continue as follows:

  • Remove the barPosition key path from the Navigation Bar’s Identity Inspector.
  • Add an IBOutlet for the Navigation Bar to your view controller.
  • Set your view controller to be a UINavigationBarDelegate.
  • Add the delegate method func position(for bar: UIBarPositioning) -> UIBarPosition to your view controller, and return a value of UIBarPosition.topAttached.
leanne
  • 7,940
  • 48
  • 77