Skip to content

Instantly share code, notes, and snippets.

@hirohitokato
Forked from akisute/UIWebView+Additions.h
Created February 28, 2011 06:30
Show Gist options
  • Save hirohitokato/847008 to your computer and use it in GitHub Desktop.
Save hirohitokato/847008 to your computer and use it in GitHub Desktop.
#import <UIKit/UIKit.h>
@interface UIWebView (Additions)
/*!
@abstract Enable/Disable the receiver from scrolling.
*/
@property (nonatomic, assign) BOOL webViewScrollEnabled;
@end
#import "UIWebView+Additions.h"
@implementation UIWebView (Additions)
- (BOOL)webViewScrollEnabled
{
// iOS 3.2 or above
// - UIWebView has inner UIScrollView
// so we can use it easily
for (id subview in self.subviews) {
if ([[subview class] isSubclassOfClass: [UIScrollView class]]) {
return ((UIScrollView *)subview).scrollEnabled;
}
}
// iOS 3.1.3 or less
// - UIWebView doesn't contain UIScrollView
// - UIWebView uses custom scroller class
// we can use its method
//
// Customized the way introduced in these page
// http://d.hatena.ne.jp/nakamura001/20090520/1242837408
// http://praveenmatanam.wordpress.com/2009/04/03/how-to-disable-uiwebview-from-user-scrolling/
//
// I'm not sure the name of the getter method, probably which is "isScrollingEnabled" or "scrollingEnabled",
// so I'm trying both of them
SEL selector = @selector(isScrollingEnabled);
for (id subview in self.subviews) {
if ([subview respondsToSelector:selector]) {
NSMethodSignature *sig = [subview methodSignatureForSelector:selector];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:sig];
[invocation setTarget:subview];
[invocation setSelector:selector];
[invocation invoke];
BOOL result = YES;
[invocation getReturnValue:&result];
return result;
}
}
selector = @selector(scrollingEnabled);
for (id subview in self.subviews) {
if ([subview respondsToSelector:selector]) {
NSMethodSignature *sig = [subview methodSignatureForSelector:selector];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:sig];
[invocation setTarget:subview];
[invocation setSelector:selector];
[invocation invoke];
BOOL result = YES;
[invocation getReturnValue:&result];
return result;
}
}
// Couldn't find the current state. Maybe it's YES (the default)
return YES;
}
- (void)setWebViewScrollEnabled:(BOOL)b
{
// iOS 3.2 or above
// - UIWebView has inner UIScrollView
// so we can use it easily
for (id subview in self.subviews) {
if ([[subview class] isSubclassOfClass: [UIScrollView class]]) {
((UIScrollView *)subview).scrollEnabled = b;
return;
}
}
// iOS 3.1.3 or less
// - UIWebView doesn't contain UIScrollView
// - UIWebView uses custom scroller class
// we can use its method
//
// Customized the way introduced in these page
// http://d.hatena.ne.jp/nakamura001/20090520/1242837408
// http://praveenmatanam.wordpress.com/2009/04/03/how-to-disable-uiwebview-from-user-scrolling/
SEL selector = @selector(setScrollingEnabled:);
for (id subview in self.subviews) {
if ([subview respondsToSelector:selector]) {
NSMethodSignature *sig = [subview methodSignatureForSelector:selector];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:sig];
[invocation setTarget:subview];
[invocation setSelector:selector];
[invocation setArgument:&b atIndex:2];
[invocation invoke];
return;
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment