Search code examples
iosswiftuiscrollviewframecontentoffset

How to move up scrollview 's frame before scrolling contentView? (when scrollView starts at the middle of the screen)


My scrollview is at the vertical center of the screen, and a view1 is above it, at y=0.

When I scroll down, the view1 is enlarged (this works fine, it creates a zoom). When I scroll up, I would like to move the scrollView up to the top of the screen (y=0), and only there, start the normal scrolling to show the rest of the contentView. So the contentView should not scroll until we reach y=0.

How could I do that?
The problem is : if I scroll up, the contentView scrolls. So I don't see how to check the contentView, because the frame will go up, but the contentView also at the same time.

Here is my method to scroll :

func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView.contentOffset.y > 0 && scrollView.frame.origin.y > 0 {
        //goes up before we scroll contentView
        scrollView.frame = CGRectOffset(scrollView.frame, 0, ........ ) //HERE
    }

    //enlarge view
    else if scrollView.contentOffset.y < 0 { //... }

Solution

  • A possible approach would be to simply "reset" the scrollview's contentOffset.y value anytime it is dragged and your frame is not yet positioned at the top. Simply try to set scrollView.contentOffset = CGPointZero in your first if-clause.

    Hope this helps!