首页 > 解决方案 > 快速显示和隐藏导航栏

问题描述

我想创建:显示没有导航栏的视图,当我滚动时,如果从顶部的距离 >= 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)
    }
}

标签: iosswiftuinavigationcontrolleruinavigationbar

解决方案


你需要的功能可以用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() 中,您可以隐藏导航栏,因此当您打开应用程序时,导航栏会被隐藏。

希望这会帮助你。


推荐阅读