ios-隐藏UINavigationBar

有的时候碰到detail页需要隐藏navigation bar的需求. 此时可用setNavigationBarHidden方法来设置. 如下:

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

setNavigationBarHidden方法时push和pop操作过度平滑. 但是发现侧滑退出功能已无效.

此时我们可以重写如下方法gestureRecognizerShouldBegin.

  1. 在class上加上UIGestureRecognizerDelegate.
  2. 设置 navigationController?.interactivePopGestureRecognizer?.delegate = self
  3. 增加如下函数:
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if(navigationController!.viewControllers.count > 1){
            return true
        }
        return false
    }

你可能感兴趣的:(ios-隐藏UINavigationBar)