2012-01-28 139 views
3

我需要知道如何停止在xcode 4中的UIwebview中滚动?在UIWebview中停止滚动

[[[webView subviews] lastobject] setScrollingEnabled:NO]; 

上面的代码不工作,因为“的NSArray”例如消息不声明一个方法具有选择“lastobject”。为什么是这样或者有没有新的代码我不知道禁用滚动?谢谢。

回答

4
UIView* row = nil; 
for(row in webView.subviews){ 
    if([row isKindOfClass:[UIScrollView class] ]){ 
     UIScrollView* scrollRow = (UIScrollView*) row; 
     scrollRow.scrollEnabled = NO; 
     scrollRow.bounces = NO; 
     scrollRow.backgroundColor=[UIColor clearColor]; 
    } 
} 

你的代码试图作出假设有关苹果定义子视图的顺序..

2

我会将webview的UserInteractionEnabled属性设置为NO,以便它无法滚动。

+0

相反,你失去了一些其他功能,这可能是有用的。所以这不是一个好主意,如果你只想禁用滚动。 – 2012-10-26 06:52:45

5

注意,与iOS 5,你必须滚动视图直接访问。

所以,如果你想保持兼容性,你可以这样做:

UIScrollView *scrollView = nil; 
if ([webView respondsToSelector:@selector(scrollView)]) { //iOS 5+ 
    scrollView = webView.scrollView; 
} else { //iOS 4- 
    for(UIView *view in webView.subviews){ 
     if([view isKindOfClass:[UIScrollView class] ]){ 
      scrollView = (UIScrollView *) view; 
      break; 
     } 
    } 
} 
scrollView.scrollEnabled = NO; 
scrollView.bounces = NO; 
scrollView.backgroundColor=[UIColor clearColor]; 
5

只需将此行添加到viewDidLoad

myWebView.scrollView.scrollEnabled = NO;