setNavigationBarHidden不起作用

时间:2018-07-29 22:58:20

标签: ios swift uinavigationcontroller uinavigationbar

对于您中的某些人来说,这似乎很明显,但是我在网上找不到任何答案。我试图使导航栏仅出现在一个View Controller上,因此所有教程都显示我刚刚添加了此内容:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.navigationController?.setNavigationBarHidden(true, animated: false)
}
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.setNavigationBarHidden(false, animated: false)
}

但是当我退出该View Controller然后返回到它时,导航栏就不在那里了。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我认为您处在正确的轨道上,但是您需要定义所采取的措施。假设您在View Controller中具有UIScrollView,并且希望导航栏消失,所以这是最好的代码。

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    if(velocity.y>0) {
        UIView.animate(withDuration: 2.5, delay: 0, options: UIViewAnimationOptions(), animations: {
            self.navigationController?.setNavigationBarHidden(true, animated: true)
        }, completion: nil)

    } else {
        UIView.animate(withDuration: 2.5, delay: 0, options: UIViewAnimationOptions(), animations: {
            self.navigationController?.setNavigationBarHidden(false, animated: true)
        }, completion: nil)
    }

}

在您的情况下,您无需两次声明该动画已由该函数定义的动画,请尝试按如下所示删除动画。

override func viewWillDisappear(_ animated: Bool) {
    self.navigationController?.setNavigationBarHidden(true, animated: false)
}
override func viewWillAppear(_ animated: Bool) {
    self.navigationController?.setNavigationBarHidden(false, animated: false)
}