2011-02-14 54 views
0

我有一个iphone UIWebView的内容是大约1000px x 200px。目前,当用手指滚动时,它会垂直反弹,但不会水平。获取UIWebView滚动和水平反弹但不垂直

我想让它做相反的事:水平反弹但不垂直。

是否有一些技巧我需要在我的html做到这一点?例如明确设置身体的宽度和高度也许?或者它是UIWebView的设置?我可以做到以下几点,完全禁用弹跳,有没有类似的设置每个方向的弹跳?

[[[webView subviews] lastObject] setBounces:NO]; 

非常感谢您的帮助

回答

0

你可以尝试添加CSS样式表,并与您的视图的尺寸设置一个div容器。并将styleSheet应用于您的UIWebView。

你可以那样做 -

NSString *cssPath = [[NSBundle mainBundle] pathForResource:@"style" ofType:@"css"]; 

     //do base url for css 
     NSString *path = [[NSBundle mainBundle] bundlePath]; 
     NSURL *baseURL = [NSURL fileURLWithPath:path]; 

     NSString *html =[NSString stringWithFormat:@"<!DOCTYPE html><html><head><link rel=\"stylesheet\" href=\"%@\" type=\"text/css\" /></head><body><section></section><br><br></body></html>",,cssPath]; 
     [webView loadHTMLString:html baseURL:baseURL]; 
0

您可以通过所有的UIWebViews中的子视图周期(使用for循环)逐项检查,看它是否是一个UIScrollView(使用isMemberOfClass:),然后更改属性在UIScrollView中满足您的需求。

for (UIView *subview in [myWebView subviews]) { 
    if ([subview isMemberOfClass:[UIScrollView class]]) { 
     [(UIScrollView *)subview setAlwaysBounceVertical:NO]; 
     [(UIScrollView *)subview setAlwaysBounceHorizontal:YES]; 
    } 
} 
+0

谢谢,我看到了这些属性,并给了他们一个尝试,但它没有帮助。 – Chris 2011-02-14 21:23:51

+0

而不是`isMemberOfClass`尝试`isKindOfClass` – 2011-02-14 21:38:35

5

在iOS 5中,您不需要访问子视图。 UIWebView包含scrollView属性嵌套的UIScrollView:

要禁用反弹:

webView.scrollView.bounces = NO; 

要只反弹垂直:

webView.scrollView.alwaysBounceVertical = NO; 
webView.scrollView.alwaysBounceHorizontal = YES; 
0

你总是可以做到:

for (UIView *subview in [self.webView subviews]) { 
    SEL sel = @selector(setAlwaysBounceHorizontal:); 
    if([subview respondsToSelector:sel]){ 
     [subview performSelector:sel withObject:(id)YES]; 
    } 
} 

在您的viewDidLoad