swift 显示和隐藏导航栏

swift show and hide Navigation Bar

我想创建:显示不带导航栏的视图,当我滚动时如果距离顶部 >= 100 高度并到底部显示导航栏。

从底部滚动时:如果到顶部的距离 <= 100 高度需要隐藏导航栏 我试试这个,但它对我没有帮助

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)
    }
}

你需要的功能你可以用scrollViewDidScroll来完成。我已经实施和测试并且它正常工作。

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print("scroll Content : \(scrollView.contentOffset.y)")

            if scrollView.contentOffset.y >= 100
            {
                UIView.animate(withDuration: 2.5, animations: {
                    self.navigationController?.setNavigationBarHidden(true, animated: true)
                })

            }
            else
            {

                UIView.animate(withDuration: 2.5, animations: {
                    self.navigationController?.setNavigationBarHidden(false, animated: true)
                })

            }

    }

在 viewDidLoad() 中,您可以隐藏导航栏,这样当您打开应用程序时,导航栏就会被隐藏。

希望对您有所帮助。