How to disable UIWebview horizontal scrolling?

前端 未结 7 1221
长情又很酷
长情又很酷 2021-02-05 14:48

I\'ve tried disabling it by inserting:


into my HTML string, and a dozen variations of th

相关标签:
7条回答
  • 2021-02-05 14:58

    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.

    Here's a very bare HTML5 skeleton that should allow vertical scrolling and disallow horizontal scrolling, including disallowing horizontal bouncing. This also disallows zooming -- it assumes that your web page should always be shown 1-1 to the user. This is useful if you're using an UIWebView to show a user interface.

    <!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>
    
    0 讨论(0)
  • 2021-02-05 15:03

    My version, a common solution:

    - (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    [webView.scrollView setContentSize: CGSizeMake(webView.frame.size.width, webView.scrollView.contentSize.height)];
    
    }
    
    0 讨论(0)
  • 2021-02-05 15:03

    Swift 5.0 solution:

    func webViewDidFinishLoad(_ webView: UIWebView) {
        webView.scrollView.contentSize = CGSize(width: webView.frame.size.width, height: webView.scrollView.contentSize.height)        
    }
    
    0 讨论(0)
  • 2021-02-05 15:09

    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. Thanks for answering anyhow.

    0 讨论(0)
  • 2021-02-05 15:15

    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:

    -(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. This fix it.

    0 讨论(0)
  • 2021-02-05 15:20

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

    0 讨论(0)
提交回复
热议问题