Vlad78
Vlad78

Reputation: 133

swift show and hide Navigation Bar

I want to create : show view without Navigation Bar, and when I scrolling if distance from top >= 100 height and to the bottom show Navigation Bar.

When scroll from bottom : if distance to top <= 100 height need to hideNavigation Bar Im try this, but it did not help me

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

Upvotes: 0

Views: 655

Answers (1)

Yash Bhikadiya
Yash Bhikadiya

Reputation: 188

The function that you need you can do with scrollViewDidScroll. I have implemented and tested and its working properly.

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

            }

    }

in viewDidLoad() you can hide the navigationbar so when you open app that time navigationbar is hidden.

Hope this will help you.

Upvotes: 1

Related Questions