0

I have a UIWebview on top of which is a UIStackView. I want to hide and show the stackview with animation on scroll in the webview like the effect in the chrome browser.

How do I achieve this?

P.S : This one didn't work

iOS/Swift - Hide/Show UITabBarController when scrolling down/up

Community
  • 1
  • 1
Aryan Sharma
  • 625
  • 2
  • 9
  • 24

2 Answers2

1

ScrollView has scrollViewDidScroll you can use it to detect scrolling and use method animate(withDuration duration: TimeInterval, animations: @escaping () -> Swift.Void) from UIView to change height of your StackView. This is probably the simplest solution.

Edit:

if you want to detect interactions earlier try scrollViewWillBeginDragging(_ scrollView: UIScrollView). If this also doesn't solve your problem try creating your own custom gesture and add it to scrollview.

Axel
  • 768
  • 6
  • 20
1

I got it to work after following Axel's advice. Here's the code:

var lastContentOffset: CGPoint!

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    lastContentOffset = scrollView.contentOffset
}

func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
    if lastContentOffset.y > scrollView.contentOffset.y {
        print("Going up!")
        if topBarStackView.isHidden == true{
            UIView.animate(withDuration: 0.2, animations: {
                self.topBarStackView.isHidden = false
            })
        }
    } else {
        print("Going down!")
        if topBarStackView.isHidden == false {
            UIView.animate(withDuration: 0.2, animations: {
                self.topBarStackView.isHidden = true
            })
        }
    }
}
Aryan Sharma
  • 625
  • 2
  • 9
  • 24