10

I am trying to change the height of there navigation bar for my app. Currently the height is fixed to 44. I can change the width from Xcode but not the height.

I have no idea how to change this. Very new to iOS development.

Can anyone please help?

Skywalker
  • 4,984
  • 16
  • 57
  • 122
  • Is it your navigation bar or a UINavigationController's navigation bar? – matt Aug 21 '15 at 14:09
  • navigation bar. simply dragged and dropped it on my view. I have no controller as such attached to the navigation bar. – Skywalker Aug 21 '15 at 14:10
  • Then you can do it with constraints (and no code is needed). See my answer below for screen shots. – matt Aug 21 '15 at 14:43
  • Possible duplicate of [how to increase the height of navigation bar in xcode?](http://stackoverflow.com/questions/31940352/how-to-increase-the-height-of-navigation-bar-in-xcode) – odm Jan 11 '17 at 22:42

3 Answers3

21

simply dragged and dropped it on my view

In that case, the simplest way is with constraints. Just give it a height constraint (along with the other constraints that position it). No code required! Here's an example:

enter image description here

That was achieved with no code at all. It's all done with constraints:

enter image description here

We are pinned to the top and sides of the superview, along with height constraint of 100.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • This is amazing thank you! Can I also reduce the height using constraints? – Skywalker Aug 21 '15 at 18:48
  • You can, but it won't look good because it's putting things like the title into place automatically, and a shorter nav bar won't have room for the title. – matt Aug 21 '15 at 18:53
  • 1
    @matt Using this way how to make the title vertically centered?? – Alfy Sep 30 '16 at 06:46
  • how do you set height constraints ? When I tried drag and drop I got only `delegate` option ... There is no height constraints as usual – Sirop4ik Feb 01 '17 at 16:41
  • @AlekseyTimoshchenko If you don't understand how to create a height constraint on a UINavigationBar, could you please ask that as a separate question? – matt Feb 01 '17 at 16:44
  • Eventually I found my issue, I was needed drag and drop not on the current view in storyboard instead of this I needed drag and drop from right side view tree – Sirop4ik Feb 01 '17 at 20:19
13

Try this :

import UIKit

class YourViewController : UIViewController {

    var navBar: UINavigationBar = UINavigationBar()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setNavBarToTheView()
        // Do any additional setup after loading the view.
        self.title = "test test"
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func setNavBarToTheView() {
        self.navBar.frame = CGRectMake(0, 0, 320, 50)  // Here you can set you Width and Height for your navBar
        self.navBar.backgroundColor = (UIColor.blackColor())
        self.view.addSubview(navBar)
    }
}
DevAndArtist
  • 4,971
  • 1
  • 23
  • 48
iAnurag
  • 9,286
  • 3
  • 31
  • 48
  • I just tried that option and theres no nav bar displaying. Should drag and drop a nav bar within my view or are we creating a nav bar programmatically? – Skywalker Aug 21 '15 at 14:16
  • did u embed your viewcontroller in UINavigationController? – iAnurag Aug 21 '15 at 14:20
  • I dont I have. I am not sure exactly how to. – Skywalker Aug 21 '15 at 14:22
  • I can see the navbar now and "test test" title but still can't change the height. I tried changing the values where you commented in the code but it had no effect. – Skywalker Aug 21 '15 at 14:28
  • 4
    From the Apple Documentation: "It is permissible to customize the appearance of the navigation bar using the methods and properties of the UINavigationBar class but you must never change its frame, bounds, or alpha values or modify its view hierarchy directly. To show or hide the navigation bar, you should always do so through the navigation controller by changing its navigationBarHidden property or calling the setNavigationBarHidden:animated: method." – iAnurag Aug 21 '15 at 14:31
1

I know this makes no sense, however this is what I did ( worked without laying down constraints ).

  1. select your View Controller.
  2. Open Show the attributes inspector
  3. for top bar select any tab bar (I'm choosing translucent Tab Bar).
  4. within the show the object library drag and drop the navigation item on the View Controller. (If done right, should look like image 3)
  5. Additionally ( fyi ), you can add a constraint to your button, etc. with using no margins and top being set to 0.

enter image description here image 2

image 3

enter image description here

dnaatwork.com
  • 362
  • 5
  • 9