简体   繁体   English

如何禁用 UIWebview 水平滚动?

[英]How to disable UIWebview horizontal scrolling?

I've tried disabling it by inserting:我尝试通过插入来禁用它:

<meta name="viewport" content="width=320" />

into my HTML string, and a dozen variations of the above in the vain hope that I just screwed up the tag syntax... but nothing seems to stop UIWebView from scrolling horizontally.进入我的 HTML 字符串,以及上面的十几种变体,希望我只是搞砸了标签语法……但似乎没有什么能阻止 UIWebView 水平滚动。 And yet there are apps that manage to do this (like MobileRSS), and presumably, since they haven't gotten rejected, they're not using private APIs.然而,有些应用程序可以做到这一点(例如 MobileRSS),并且大概因为它们没有被拒绝,所以它们没有使用私有 API。

My version, a common solution:我的版本,一个常见的解决方法:

- (void)webViewDidFinishLoad:(UIWebView *)webView {

[webView.scrollView setContentSize: CGSizeMake(webView.frame.size.width, webView.scrollView.contentSize.height)];

}

I eventually managed to solve this.我最终设法解决了这个问题。 I'd already tried setting the viewport width value, and it didn't work.我已经尝试过设置视口宽度值,但没有奏效。 What DID work was using overflow:hidden in conjunction with setting the viewport width.什么 DID 工作是使用 overflow:hidden 结合设置视口宽度。 Thanks for answering anyhow.无论如何感谢您的回答。

if the webView content a word which has length greater than the webView.如果 webView 包含一个长度大于 webView 的单词。 you have to break that string.你必须打破那根弦。 so use the below code in the div所以在div中使用下面的代码

word-wrap: break-word;自动换行:断字;

Your web page needs to have two attributes: 1 -- the viewport meta tag and 2-- a width of 320px or less, including margin and padding.您的网页需要有两个属性:1——视口元标记和 2——320px 或更小的宽度,包括边距和填充。

Here's a very bare HTML5 skeleton that should allow vertical scrolling and disallow horizontal scrolling, including disallowing horizontal bouncing.这是一个非常简单的 HTML5 框架,它应该允许垂直滚动和禁止水平滚动,包括禁止水平弹跳。 This also disallows zooming -- it assumes that your web page should always be shown 1-1 to the user.这也不允许缩放——它假定您的网页应始终以 1-1 的方式显示给用户。 This is useful if you're using an UIWebView to show a user interface.如果您使用 UIWebView 来显示用户界面,这将非常有用。

<!doctype html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=0" />
    <style>
        body {width:320px;margin:0;padding:0;}
    </style>
</head>
<body></body>
</html>

Set the delegate for the UIWebview 's UIScrollview (on iOS5 by webview.scrollView and on iOS4 by traversing the subview tree and selecting the UIScrollview subview) to 'self' and use the following code:UIWebviewUIScrollview的委托(在 iOS5 上通过webview.scrollView和在 iOS4 上通过遍历子视图树并选择UIScrollview子视图)设置为“self”并使用以下代码:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [scrollView setContentOffset:CGPointMake(0, scrollView.contentOffset.y)];
}

Works perfectly, for some reason my HTML code was overflowing horizontally without any need for it.完美运行,出于某种原因,我的 HTML 代码水平溢出,不需要它。 This fix it.这修复它。

<meta name="viewport" content="width=device-width, user-scalable=no initial-scale=1.0" />

Swift 5.0 solution: Swift 5.0解决方案:

func webViewDidFinishLoad(_ webView: UIWebView) {
    webView.scrollView.contentSize = CGSize(width: webView.frame.size.width, height: webView.scrollView.contentSize.height)        
}

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

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