How to Increase the Height of Navigation Bar in Xcode

How can I change height of Navigation Bar - Swift 3

UPDATE 8/3/20: I posted this in 2016. A number of people have stated this no longer works so please use at your own risk. I am not working in iOS at the moment so I do not have an update handy. Best of luck!

Here is one way to do it:

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

}

How to change navigationBar height in iOS 11?

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
Sample Image

Xcode 8
Sample Image

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.

Increase NavigationBar height

Try this code:

Note: Code tested in Swift 3.

Answer 1: Updated Answer

class ViewController: UIViewController {

var customBar: UINavigationBar = UINavigationBar()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

//Title
title = "Some Title"

// Add bar button item
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(addTapped))

self.customBar.frame = CGRect(x:0, y:0, width:view.frame.width, height:(navigationController?.navigationBar.frame.height)! + 50)
self.customBar.backgroundColor = UIColor.green
self.view.addSubview(customBar)
}

func addTapped() {

print("Button Pressed")

}

Output:

Sample Image


Answer 2:

override var isViewLoaded: Bool {

// Add bar button item
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(addTapped))

//Vertical and Horizonal barButtonItem position offset
navigationItem.leftBarButtonItem?.setTitlePositionAdjustment(UIOffset(horizontal: 0, vertical: 20), for: UIBarMetrics.default)

navigationItem.rightBarButtonItem?.setTitlePositionAdjustment(UIOffset(horizontal: 0, vertical: 20), for: UIBarMetrics.default)

return true
}

func addTapped() {

print("Button Pressed")

}

Note: Above code only works in isViewLoaded: Bool method.But, No luck.When, I tried this code in other viewLoad method.

Output 1: barButtonItem moved 20 pixel up vertically.

Sample Image

Output 2: barButtonItem moved 20 pixel down vertically.

Sample Image

Hope, Above code fix your problem.

Change Navigation Bar Height

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.
Sample Image



Related Topics



Leave a reply



Submit