导航栏 - 重新启动应用

时间:2017-04-08 15:23:44

标签: swift swift3 uinavigationcontroller uinavigationbar

我为导航栏设置了自定义高度,状态栏也隐藏了:

hidden status bar

override func viewDidLayoutSubviews() {
    let bounds = self.navigationController!.navigationBar.bounds
    self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: 60)
}
override var prefersStatusBarHidden: Bool {
    return true
}

我也尝试过:

override func viewDidAppear(_ animated: Bool) {
    self.navigationController!.navigationBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 40)

}

但重新启动应用后,顶部有一种填充:

enter image description here

我看到了answer

override func viewWillAppear(_ animated: Bool) {
    let bounds = self.navigationController!.navigationBar.bounds
    navigationController?.isNavigationBarHidden = true
    let navBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: bounds.width, height: 60))
    self.view.addSubview(navBar);
    let navItem = UINavigationItem(title: "Add");
    let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: nil, action: #selector(addNewRecipe(_:)));
    navItem.rightBarButtonItem = doneItem;
    navBar.setItems([navItem], animated: false);
    view.addSubview(navBar)
}

除了隐藏导航栏并以编程方式编写每个导航项外,还有其他方法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

详细

xCode 8.3,Swift 3.1

完整样本

  

.aspx
  

Main.storyboard

UID00000004

enter image description here

结果

enter image description here