Changing the height of the Navigation bar iOS Swift

放肆的年华 提交于 2019-11-26 07:45:51

问题


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?


回答1:


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)
    }
}



回答2:


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:

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

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




回答3:


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.



来源:https://stackoverflow.com/questions/32142375/changing-the-height-of-the-navigation-bar-ios-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!