在UIWebview中停止滚动

时间:2012-01-28 02:32:10

标签: iphone objective-c ios xcode

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

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

上面的代码不起作用,因为“NSarray”例如消息没有声明带有选择器'lastobject'的方法。为什么这个或者是否有我不知道禁用滚动的新代码?感谢。

4 个答案:

答案 0 :(得分:5)

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];
    }
}

您的代码正试图对Apple正在定义的子视图的顺序做出假设。

答案 1 :(得分: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];

答案 2 :(得分:5)

只需将此行添加到viewDidLoad

myWebView.scrollView.scrollEnabled = NO;

答案 3 :(得分:2)

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

相关问题