The first and only subview of the UIWebView is a UIScrollView. Unfortunately, Apple has provided no direct access to this scrollview; however, the Apple engineers also do not yet have any notion of private methods on objects. ;) (You may have heard stories from other developers about private APIs. However, an API is only private if its methods cannot be found in Apple's published APIs. All of the methods that we are going to use here are included in the documented APIs.)
First we will pull the UIScrollView out from the UIWebView:
That's it, now you have full control over the webviews inner scrollview and we have only used documented API's.
NSArray *wsv = [NSArray arrayWithArray:[webScroller subviews]];
[[wsv objectAtIndex:6] setHidden:YES];
[[wsv objectAtIndex:7] setHidden:YES];
[[wsv objectAtIndex:8] setHidden:YES];
[[wsv objectAtIndex:9] setHidden:YES];
NSArray *sv = [NSArray arrayWithArray:[myWebivew subviews]];
UIScrollView *webScroller = (UIScrollView *)[sv objectAtIndex:0];
UIScrollView *webScroller = (UIScrollView *)[sv objectAtIndex:0];
That's it, now you have full control over the webviews inner scrollview and we have only used documented API's.
(The sv array provides a pleasant level of indirection as it allows us to access the scrollview without directly pulling it from the webview.) If you are looking to prevent scrolling, control scroll offset or effect any other scrolling behavior you will be able to do it with this webScroller object.
Next if you want to remove those shadows from the top and bottom of the webview, you can simply hide them. They are inserted in the webScroller (the webview's scrollview). Use the following lines to hide the shadows.
NSArray *wsv = [NSArray arrayWithArray:[webScroller subviews]];
[[wsv objectAtIndex:6] setHidden:YES];
[[wsv objectAtIndex:7] setHidden:YES];
[[wsv objectAtIndex:8] setHidden:YES];
[[wsv objectAtIndex:9] setHidden:YES];
There you have it!
Remember, as sdk version change there may be some issues with this code, so watch your apps to make sure they continue to function properly when new OS version are released.
2 comments: