iOS 11中导航栏的自定义高度

时间:2017-09-30 16:11:56

标签: ios swift xcode

我试图为导航栏设置自定义高度。我在iOS 11,Xcode 8中运行的代码很好;但现在,我有Xcode 9和iOS 11,但代码无效。这就是我所拥有的......

var navBar: UINavigationBar = UINavigationBar()

override func viewDidLoad() {
    super.viewDidLoad()

    self.navBar.frame = CGRect(x: 0, y: 0, width: view.frame.width, height:82) 
    view.addSubview(navBar)
}


override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    self.navBar.frame = CGRect(x: 0, y: 0, width:self.view.frame.width, height: 82)
}

感谢您的帮助!!!

2 个答案:

答案 0 :(得分:0)

你必须设置

self.navBar.frame = CGRect(x: 0, y: 0, width: (bounds?.width)!, height: (bounds?.height)! + height)

答案 1 :(得分:0)

我找到了解决方案!!!

我搜索过,我在Apple文档中找到了一个示例。我只是这样做了:

- 我创建了一个类型为UIView的类,我推荐了这个:

class ExtendedNavBarView: UIView {

override func willMove(toWindow newWindow: UIWindow?) {
    super.willMove(toWindow: newWindow)

    layer.shadowOffset = CGSize(width: 0, height: CGFloat(1) / UIScreen.main.scale)
    layer.shadowRadius = 0

    layer.shadowColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1).cgColor
    layer.shadowOpacity = 0.25
}

}

- 然后,我把它放在viewDidLoad:

    navBar.shadowImage = #imageLiteral(resourceName: "TransparentPixel")

    let extendedBar = ExtendedNavBarView()
    extendedBar.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: 82)
    view.addSubview(extendedBar)

    extendedBar.backgroundColor = UIColor(red: 249/255, green: 249/255, blue: 249/255, alpha: 1.0)

    self.navBar.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 82)
    view.addSubview(navBar)