Search code examples
iosswiftuiscrollview

How to hide/show a uiview when uiscrollview Y position changes?


I have a uiview in my app. I want to show the uiview when uiscrollview Y position changes. I tried some stackoverflow suggestion but can't get the expected result. Here's my code that i tried:

if (topScrollView.contentOffset.y) >= 30{
   slideInSearchView.isHidden = false
} else {
   slideInSearchView.isHidden = true
}

Solution

  • Make sure code is inside scrollViewDidScroll

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if (topScrollView.contentOffset.y) >= 30{
            slideInSearchView.isHidden = false
        } else {
            slideInSearchView.isHidden = true
        }
    }