Get UIWebView to scroll and bounce horizontally but not vertically
I have an iphone UIWebView with contents that are approx 1000px x 200px. Currently when scrolling with your finger, it'll bounce vertically but not horizontally.
I want it to do the opposite: bounce horizontally but not vertically.
Is there some trickery i nee开发者_如何学Cd to do in my html to achieve this? Eg setting the width and height on the body explicitly maybe? Or is it a setting of the UIWebView? I can do the following to totally disable bouncing, is there something similar to set bouncing on a per-direction basis?
[[[webView subviews] lastObject] setBounces:NO];
Thanks a lot for the help
In iOS 5, you don't need to access the subviews. UIWebView
contains a scrollView
property to the nested UIScrollView:
To disable bounce:
webView.scrollView.bounces = NO;
To only bounce vertically:
webView.scrollView.alwaysBounceVertical = NO;
webView.scrollView.alwaysBounceHorizontal = YES;
You can try to add a CSS StyleSheet and set the a container Div with the dimension of your view. and apply the styleSheet to your UIWebView.
You can do it like that -
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];
You can cycle through all the UIWebViews subviews(using a for loop) checking each one to see if it's a UIscrollView (using isMemberOfClass:
) and then change the properties in the UIScrollView to meet your needs.
for (UIView *subview in [myWebView subviews]) {
if ([subview isMemberOfClass:[UIScrollView class]]) {
[(UIScrollView *)subview setAlwaysBounceVertical:NO];
[(UIScrollView *)subview setAlwaysBounceHorizontal:YES];
}
}
You can always do:
for (UIView *subview in [self.webView subviews]) {
SEL sel = @selector(setAlwaysBounceHorizontal:);
if( [subview respondsToSelector:sel] ){
[subview performSelector:sel withObject:(id)YES];
}
}
in your viewDidLoad
精彩评论