I am newbie in Swift. I am trying to change (increase) the height of the navigation bar. Is there any obvious way? (I tried to change it with CGRectMake but it generated a rect over the bar and i lost the back button etc.) Which is the best practice to implement?
Asked
Active
Viewed 4,053 times
2
-
See here http://stackoverflow.com/questions/32142375/changing-the-height-of-the-navigation-bar-ios-swift – Twitter khuong291 Apr 19 '16 at 12:11
-
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
4 Answers
0
you should not try to change navigation bar's size according to apple guideline. you should use custom navigation bar if required. hope this will help :)

Ketan Parmar
- 27,092
- 9
- 50
- 75
0
We can make use of Inheritance Method.
1) Make a parent controller of UIViewController. Name it UIAppViewController.
// UIAppViewController.swift
import UIKit
class UIAppViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
}
func addCustomNavigationBarToView()
{
// Create the navigation bar
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 84))
// Create a navigation item with a title
let navigationItem = UINavigationItem()
navigationItem.title = "Title"
// Create left and right button for navigation item
let leftButton = UIBarButtonItem(title: "Save", style: UIBarButtonItemStyle.Plain, target: self, action: "saveButtonClicked:")
let rightButton = UIBarButtonItem(title: "Right", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
// Create two buttons for the navigation item
navigationItem.leftBarButtonItem = leftButton
navigationItem.rightBarButtonItem = rightButton
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Note that I have made a method in which a custom Navigation Bar is added to self.view.
2) On ViewController:
// ViewController.swift
import UIKit
class ViewController: UIAppViewController,UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
self.navigationController?.navigationBarHidden = false
self.addCustomNavigationBarToView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

Alvin George
- 14,148
- 92
- 64
0
Sub classing UINavigationBar is also a solution.
File-New-File- iOS Source - Cocoa Touch class- Select Subclass of : UINavigationBar and Name it - CustomNavigationBar.
class CustomNavigationBar: UINavigationBar {
override init(frame: CGRect) {
super.init(frame: frame)
//Change here
self.backgroundColor = UIColor.redColor()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
override func drawRect(rect: CGRect) {
//set frame.
}
}

Alvin George
- 14,148
- 92
- 64
-1
try this code:
func sizeThatFits(size: CGSize) -> CGSize
{
var newSize: CGSize = CGSizeMake(self.frame.size.width, 70)
return newSize
}

Abhinandan Pratap
- 2,142
- 1
- 18
- 39