IOS How to restrict scrolling of UIScrollview to a limited extent?

What is the best way to set left / right scroll restrictions on a UIScrollView. I would have thought it would be easy, but all my attempts were unsuccessful.

So, to be clear, I need a solution that allows me to programmatically limit the scroll size whenever I need while using my application. This will most often respond to changes in the displayed data.

Thanks,
Doug

+7
source share
3 answers

I went with my approach to setting carriage restrictions in my overloaded layoutSubviews method.

Pseudo Code:

Calculate criteria for constraining pan if (pan-constraint-is-met) { Calculate pan-limit [self setContentOffset:limitedContentOffset]; } 
-4
source

You can control the size of the content using the scroll contentSize property. If this is not enough for you (for example, you need to limit the scroll area to some arbitrary area in the middle of your content), you can make contentOffset be in the required limit in the delegate method of your scroll view.

Basically, the code might look like this:

 - (void) scrollViewDidScroll:(UIScrollView*)scroll{ CGPoint offset = scroll.contentOffset; // Check if current offset is within limit and adjust if it is not if (offset.x < minOffsetX) offset.x = minOffsetX; if (offset.y < minOffsetY) offset.y = minOffsetY; if (offset.x > maxOffsetX) offset.x = maxOffsetX; if (offset.y > maxOffsetY) offset.y = maxOffsetY; // Set offset to adjusted value scroll.contentOffset = offset; } 
+11
source

All you have to do is resize the content to scroll using the following code.

[scrollView setContentSize:CGSizeMake(width, height)];

+7
source

All Articles