ios 在允许的 UIWebView 上禁用滚动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6437911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:28:55  来源:igfitidea点击:

Disable Scroll on a UIWebView allowed?

iosscrolluiwebview

提问by jsaddour

I have to show a web article with a UITableViewunder the article.

我必须在文章UITableView下方显示一个网络文章。

The only option I found was to display the article in a UIWebViewin the tableView header.

我发现的唯一选择是UIWebView在 tableView 标题中的 a中显示文章。

In order to do that I have to get the height of the webView content and I have to disable scrolling for the webView.

为此,我必须获取 webView 内容的高度,并且必须禁用 webView 的滚动。

I found two solutions to disable scrolling:

我找到了两种禁用滚动的解决方案:

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

or in JavaScript:

或在 JavaScript 中:

<script type="text/javascript">
touchMove = function(event) {
    event.preventDefault();
}

I heard that the first solution is forbidden by Apple but I don't have any proof of it. Will my application be rejected by using this solution? If so, can I use the second solution without being rejected?

我听说苹果公司禁止第一个解决方案,但我没有任何证据。使用此解决方案会拒绝我的申请吗?如果是这样,我可以使用第二种解决方案而不会被拒绝吗?

回答by Alex Stanciu

Starting with iOS5 we have direct access to the scrollview of the UIWebView.
You can disable scrolling and bounces like this:

从 iOS5 开始,我们可以直接访问 UIWebView 的滚动视图。
您可以像这样禁用滚动和弹跳:

webView.scrollView.scrollEnabled = NO; 
webView.scrollView.bounces = NO;

回答by Ryan Copley

Since this question is still applicable, there is a new way to do it! (iOS 7.0+, perhaps iOS 6 also, not confirmed)

由于这个问题仍然适用,有一个新的方法来做!(iOS 7.0+,也许还有 iOS 6,未确认)

webView.scrollView.scrollEnabled = NO;

Take care :)

小心 :)

回答by Eshwar Chaitanya

You can simply set the web view user interaction property to no i.e.

您可以简单地将 web 视图用户交互属性设置为 no ie

webView.userInteractionEnabled = NO;

Its pretty simple and works fine,thanks :)

它非常简单并且工作正常,谢谢:)

回答by mturhan55

for all ios versions you can use this code instead of setScrollEnabled :

对于所有 ios 版本,您可以使用此代码而不是 setScrollEnabled :

for (UIView *view in webview.subviews) {
    if ([view isKindOfClass:[UIScrollView class]]) {
       UIScrollView *scrollView = (UIScrollView *)view;
       scrollView.scrollEnabled = NO;
    }
}

回答by Gobi M

Use this one <body ontouchmove="event.preventDefault()">

用这个 <body ontouchmove="event.preventDefault()">

回答by Ahmed Safadi

SWIFT 3 version

SWIFT 3 版本

    webView.scrollView.bounces = false
    webView.scrollView.isScrollEnabled = false

回答by Martin

I think the Javascript

我认为 Javascript

<script type="text/javascript">
touchMove = function(event) {
event.preventDefault();
}

is far the best solution.

是最好的解决方案。

Indeed :

的确 :

Your first examle, or the

你的第一个考试,或者

[[[WebView subviews] lastObject] setScrollingEnabled:NO];

one (same things) could be rejected because not documented, or make you app crash in a further ios update.

一个(同样的事情)可能会因为没有记录而被拒绝,或者让你的应用程序在进一步的 ios 更新中崩溃。

And the

[[myWebView scrollView] setBounces:NO];

method won't work with iOS < 5.

方法不适用于 iOS < 5。

回答by theiOSDude

[[[WebView subviews] lastObject] setScrollingEnabled:NO];

that sorted it for me

为我整理了它

回答by Jane Sales

The first is rejected by Apple. It happened to me just this morning.

第一个被苹果拒绝。就在今天早上发生在我身上。

回答by mafonya

[[myWebView scrollView] setBounces:NO];

[[myWebView scrollView] setBounces:NO];