简体   繁体   English

UIWebView 滚动

[英]UIWebView Scrolling

I load an HTML form in my UIWebView and it so happens that my UIWebView starts from the middle of the view and extends.我在我的 UIWebView 中加载了一个 HTML 表单,碰巧我的 UIWebView 从视图的中间开始并延伸。 I have to lock this webView from scrolling and put it on top of a scrollView to allow scrolling.我必须从滚动锁定这个 webView 并将它放在滚动视图的顶部以允许滚动。 The webView shouldn't scroll internally. webView 不应在内部滚动。 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.但是当我在其中加载一个点击了文本字段的表单时,我无法阻止 webView 在内部滚动。 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.我想阻止 webView 自动滚动并使用位于 webView 下方的 scrollView 自己处理滚动。 It might not make sense to most of you that I am reinventing 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.或者你需要停止 webview 的弹跳,你可以试试这个。

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 (ie in the proper delegate method):尝试在 webview 加载后运行它(即在正确的委托方法中):

[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:如果您有权访问 HTML,您也可以在 body 元素上设置正确的样式:

body { overflow: hidden; }

If you do either of these, you need to make sure that you make the UIWebView tall enough to display everything.如果您执行其中任何一项操作,您需要确保 UIWebView 足够高以显示所有内容。 You can get the height by running this (also after the webview has loaded):您可以通过运行它来获得高度(也在 webview 加载之后):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM