开发者

UIWebView Scrolling

开发者 https://www.devze.com 2023-03-02 05:37 出处:网络
I load an HTML form in my UIWebView and it so happens that my UIWebView starts from the middle of the view and extends. I have to lock this webView from scrolling and put it on top of a scrollView to

I load an HTML form in my UIWebView and it so happens that my UIWebView starts from the middle of the view and extends. I have to lock this webView from scrolling and put it on top of a scrollView to allow scrolling. The webView shouldn't scroll internally. I have tried this

 [[webView.subviews objectAtIndex:0] setScrollEnabled:NO];

but I am not able to stop the webView from scrolling internally when I load a form in it that has textFields clicking on which brings up the keyboard. When I load plaintext, I am able to get the desired behavior. I want to stop the webView from autoscrolling and handle the scrolling myself using the scrollView that lay beneath the webView. It might not make sense to most of you that I am reinv开发者_开发问答enting the wheel but the requirement is such.

Can anybody suggest what to do?


You can try,

for (id subview in webView.subviews)

    if ([[subview class] isSubclassOfClass: [UIScrollView class]])

        if([subview respondsToSelector:@selector(setScrollingEnabled:)]) [subview performSelector:@selector(setScrollingEnabled:) withObject:NO];

or you need to stop bouncing of the webview you can try this.

for (id subview in webView.subviews)
        if ([[subview class] isSubclassOfClass: [UIScrollView class]])
            ((UIScrollView *)subview).bounces = NO;

Hope this helps.


Try running this after the webview has loaded (i.e. in the proper delegate method):

[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.overflow='hidden'"]

If you have access to the HTML, you could also just set the correct style on the body element:

body { overflow: hidden; }

If you do either of these, you need to make sure that you make the UIWebView tall enough to display everything. You can get the height by running this (also after the webview has loaded):

CGFloat height = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"]
0

精彩评论

暂无评论...
验证码 换一张
取 消