我想隐藏导航栏时将视图的背景颜色设置为黑色,并在显示导航栏时将其设置为白色。
属性hidesBarsOnTap
在true
中设置为viewDidLoad
。这很好用:
navigationController?.hidesBarsOnTap = true
如果隐藏栏并显示栏,我如何得到通知?
答案 0 :(得分:2)
class ViewController: UIViewController {
var hidden = false {
didSet {
if let nav = navigationController {
nav.setNavigationBarHidden(hidden, animated: true)
nav.setToolbarHidden(hidden, animated: true)
view.backgroundColor = hidden ? UIColor.blackColor() : UIColor.whiteColor()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
let recognizer = UITapGestureRecognizer(target: self, action: "tap:")
view.addGestureRecognizer(recognizer)
}
func tap(recognizer: UITapGestureRecognizer) {
if recognizer.state == .Ended {
hidden = !hidden
}
}
}
答案 1 :(得分:0)
由于hidesBarsOnTap
的类型为boolean,我们可以轻松地使用它来检查并将其用作选项,如下例所示:
var set : Bool = navigationController?.hidesBarsOnTap //true or false
if (set){
//do what you want when set
}else{
//do what you want when it is not set
}