0

How to change UINavigationBar height with code int iOS8 swift?

I've tried using:

UINavigationBar.resizableSnapshotViewFromRect

UINavigationBar.drawViewHierarchyInRect

UINavigationBar.frameForAlignmentRect

None of these seem to accomplish this.

Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
Roger Zhang
  • 11
  • 1
  • 1

1 Answers1

3

Here is example for you:

import UIKit

class BaseViewConroller: UIViewController {
var navBar:UINavigationBar=UINavigationBar()

override func viewDidLoad() {
    super.viewDidLoad()
    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()
{
    navBar.frame=CGRectMake(0, 0, 320, 50)  // Here you can set you Width and Height for your navBar
    navBar.backgroundColor=(UIColor .blackColor())
    self.view .addSubview(navBar)
}

}
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
  • thank you Kheni, but I want to change the navBar height, not add a new one . for examle , when mobile Rotate screen, the navBar height changed from 80 to 40 . – Roger Zhang Nov 11 '14 at 19:06
  • 3
    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." – Michael Campsall Nov 26 '14 at 18:53