ios 阻止 UIWebView 垂直“弹跳”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/500761/
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
Stop UIWebView from "bouncing" vertically?
提问by Brad Parks
Does anyone know how to stop a UIWebView from bouncing vertically? I mean when a user touches their iphone screen, drags their finger downwards, and the webview shows a blank spot above the web page I had loaded?
有谁知道如何阻止 UIWebView 垂直弹跳?我的意思是当用户触摸他们的 iphone 屏幕,向下拖动他们的手指时,webview 会在我加载的网页上方显示一个空白点?
I've looked at the following possible solutions, but none of them worked for me:
我查看了以下可能的解决方案,但没有一个对我有用:
http://forums.macrumors.com/showthread.php?t=619534
http://forums.macrumors.com/showthread.php?t=619534
回答by Mirek Rusin
for (id subview in webView.subviews)
if ([[subview class] isSubclassOfClass: [UIScrollView class]])
((UIScrollView *)subview).bounces = NO;
...seems to work fine.
...似乎工作正常。
It'll be accepted to App Store as well.
它也将被 App Store 接受。
Update: in iOS 5.x+ there's an easier way - UIWebView
has scrollView
property, so your code can look like this:
更新:在 iOS 5.x+ 中,有一种更简单的方法 -UIWebView
具有scrollView
属性,因此您的代码可以如下所示:
webView.scrollView.bounces = NO;
Same goes for WKWebView
.
也一样WKWebView
。
回答by Brad Parks
I was looking at a project that makes it easy to create web apps as full fledged installable applications on the iPhone called QuickConnect, and found a solution that works, if you don't want your screen to be scrollable at all, which in my case I didn't.
我正在研究一个项目,该项目可以轻松地在 iPhone 上将 Web 应用程序创建为成熟的可安装应用程序QuickConnect,并找到了一个有效的解决方案,如果您根本不希望您的屏幕可滚动,就我而言我没有。
In the above mentioned project/blog post, they mention a javascript function you can add to turn off the bouncing, which essentially boils down to this:
在上面提到的项目/博客文章中,他们提到了一个可以添加来关闭弹跳的 javascript 函数,这基本上归结为:
document.ontouchmove = function(event){
event.preventDefault();
}
If you want to see more about how they implement it, simply download QuickConnect and check it out.... But basically all it does is call that javascript on page load... I tried just putting it in the head of my document, and it seems to work fine.
如果您想了解更多有关他们如何实现它的信息,只需下载 QuickConnect 并查看它.... 但基本上它所做的只是在页面加载时调用该 javascript... 我试着把它放在我的文档的头部,它似乎工作正常。
回答by Zigglzworth
Well all I did to accomplish this is :
好吧,我为实现这一目标所做的一切是:
UIView *firstView = [webView.subviews firstObject];
if ([firstView isKindOfClass:[UIScrollView class]]) {
UIScrollView *scroll = (UIScrollView*)firstView;
[scroll setScrollEnabled:NO]; //to stop scrolling completely
[scroll setBounces:NO]; //to stop bouncing
}
Works fine for me... Also, the ticked answer for this question is one that Apple will reject if you use it in your iphone app.
对我来说很好用......另外,如果你在你的 iPhone 应用程序中使用这个问题,苹果会拒绝这个问题的答案。
回答by jstr
In the iOS 5 SDK you can access the scroll view associated with a web view directly rather than iterating through its subviews.
在 iOS 5 SDK 中,您可以直接访问与 Web 视图关联的滚动视图,而不是遍历其子视图。
So to disable 'bouncing' in the scroll view you can use:
因此,要在滚动视图中禁用“弹跳”,您可以使用:
myWebView.scrollView.bounces = NO;
See the UIWebView Class Reference.
请参阅UIWebView 类参考。
(However if you need to support versions of the SDK before 5.0, you should follow Mirek Rusin's advice.)
(但是,如果您需要支持 5.0 之前的 SDK 版本,则应遵循Mirek Rusin 的建议。)
回答by Ego Slayer
Swift 3
斯威夫特 3
webView.scrollView.bounces = false
回答by Raf
Warning. I used setAllowsRubberBanding: in my app, and Apple rejected it, stating that non-public API functions are not allowed (cite: 3.3.1)
警告。我在我的应用程序中使用了 setAllowsRubberBanding:,但 Apple 拒绝了它,指出不允许使用非公共 API 函数(引用:3.3.1)
回答by Jugal K Balara
In Swift to disable bounces
在 Swift 中禁用反弹
webViewObj.scrollView.bounces = false
回答by il Malvagio Dottor Prosciutto
On iOS5 only if you plan to let the users zoom the webview contents (e.i.: double tap) the bounce setting isn't enough. You need to set also alwaysBounceHorizontal and alwaysBounceVertical properties to NO, else when they zoom-out (another double tap...) to default it will bounce again.
在 iOS5 上,仅当您打算让用户缩放 webview 内容(即:双击)时,反弹设置是不够的。您还需要将 alwaysBounceHorizontal 和 alwaysBounceVertical 属性设置为 NO,否则当它们缩小(再次双击...)默认时,它会再次反弹。
回答by Gavin Maclean
Brad's method worked for me. If you use it you might want to make it a little safer.
布拉德的方法对我有用。如果你使用它,你可能想让它更安全一些。
id scrollView = [yourWebView.subviews objectAtIndex:0]; if( [scrollView respondsToSelector:@selector(setAllowsRubberBanding:)] ) { [scrollView performSelector:@selector(setAllowsRubberBanding:) withObject:NO]; }
If apple changes something then the bounce will come back - but at least your app won't crash.
如果苹果改变了一些东西,那么反弹就会回来——但至少你的应用不会崩溃。
回答by pedro
I traversed the collection of UIWebView's subviews and set their backgrounds to [UIColor blackColor], the same color as the webpage background. The view will still bounce but it will not show that ugly dark grey background.
我遍历了 UIWebView 的子视图集合,并将它们的背景设置为 [UIColor blackColor],与网页背景颜色相同。视图仍然会反弹,但不会显示丑陋的深灰色背景。