This question already has an answer here:
I have a UIScrollView which has subviews that create a content with a width of about 7000. Now I want the UIScrollView to not scroll any further then 2000.
I tried:
[scrollView setContentSize:CGSizeMake(768.0, 2000.0)];
[scrollView setClipsToBounds:YES];
But I can still scroll to the end, how do I prevent this?
You can set content offset to 2000 if it is more than 2000 so it won't go beyond the bound.
Implement this method and set it to the delegate of that scrollview
Edit: I just tried this, and it works fine. Maybe check why
setContentSize:
is not working? Another approach, make a view with heigh of 2000, put your content view inside it, and add that view as subview of scrollview.It's seems that
webView
is changing itsscrollView
contentSize
after page loading.In your init set self as a delegate of
WebView
:And add this method:
In my test case it's working fine.
UPDATED:
Calling javascript methods sometimes does not trigger
webViewDidFinishLoad
. So the most universal technique will be Key-Value Observing.In init:
And somewhere this method:
Do not forget to remove observer in case of dealloc:
I believe your code above should work fine. setContentSize will prevent the contentOffset from getting larger. You may have to start, though, by setting contentOffset yourself (once) if the user has already scrolled outside that area. (Try resetting it to (0, 0), for example.)
Also, are you permitting zooming? If you have a zoom != 1, the content size may not be what you think it is. Try setting zoom to 1 before modifying the content size.
Hard to give specific answer without seeing your html but I've had a similar problem before and i think its actually your UIWebview thats actually scrolling and not the scrollview. somewhere on the page in the UIWebview html you will most probably have oversize content. THe webview naturally wants to scroll. In my case it was a long URL in a paragraph of text. I have also seen this happening vertically.
You need to find the container element to your oversize html and set the overflow to hidden in the css. It may be that this is the body tag or the html tag in the html or you need to wrap your content.
ie;
Failing that you may have to play with the following tag
Or set the container div to width:100% & height:100%;
Unless I'm misunderstanding something in your question this is straightforward. Add this to your view controller containing the web view.
Ok So first step, your view controller must implement UISCrollViewDelegate, and you must set your controller as a delegate for your UIScrollView.
Then, implement this delegate method in your controller:
Anytime you get pass 2000 / stops value, the scrollview should be brought back