How to set viewport meta for iPhone that handles rotation properly?

前端 未结 9 1704
情话喂你
情话喂你 2020-11-27 10:11

So I\'ve been using:


to get my HTML c

相关标签:
9条回答
  • 2020-11-27 10:48

    For anybody still interested:

    http://wiki.phonegap.com/w/page/16494815/Preventing-Scrolling-on-iPhone-Phonegap-Applications

    From the page:

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

    This instructs Safari to prevent the user from zooming into the page with the "pinch" gesture and fixes the width of the view port to the width of the screen, which ever orientation the iPhone is in.

    0 讨论(0)
  • 2020-11-27 10:53

    I had this issue myself, and I wanted to both be able to set the width, and have it update on rotate and allow the user to scale and zoom the page (the current answer provides the first but prevents the later as a side-effect).. so I came up with a fix that keeps the view width correct for the orientation, but still allows for zooming, though it is not super straight forward.

    First, add the following Javascript to the webpage you are displaying:

     <script type='text/javascript'>
     function setViewPortWidth(width) {
      var metatags = document.getElementsByTagName('meta');
      for(cnt = 0; cnt < metatags.length; cnt++) { 
       var element = metatags[cnt];
       if(element.getAttribute('name') == 'viewport') {
    
        element.setAttribute('content','width = '+width+'; maximum-scale = 5; user-scalable = yes');
        document.body.style['max-width'] = width+'px';
       }
      }
     }
     </script>
    

    Then in your - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation method, add:

    float availableWidth = [EmailVC webViewWidth];
    NSString *stringJS;
    
    stringJS = [NSString stringWithFormat:@"document.body.offsetWidth"];
    float documentWidth = [[_webView stringByEvaluatingJavaScriptFromString:stringJS] floatValue];
    
    if(documentWidth > availableWidth) return; // Don't perform if the document width is larger then available (allow auto-scale)
    
    // Function setViewPortWidth defined in EmailBodyProtocolHandler prepend
    stringJS = [NSString stringWithFormat:@"setViewPortWidth(%f);",availableWidth];
    [_webView stringByEvaluatingJavaScriptFromString:stringJS];
    

    Additional Tweaking can be done by modifying more of the viewportal content settings:

    http://www.htmlgoodies.com/beyond/webmaster/toolbox/article.php/3889591/Detect-and-Set-the-iPhone--iPads-Viewport-Orientation-Using-JavaScript-CSS-and-Meta-Tags.htm

    Also, I understand you can put a JS listener for onresize or something like to trigger the rescaling, but this worked for me as I'm doing it from Cocoa Touch UI frameworks.

    Hope this helps someone :)

    0 讨论(0)
  • 2020-11-27 10:54

    Why not just reload the page when the user rotates the screen with javascript

    function doOnOrientationChange()
    {
    location.reload();
    }
    
    window.addEventListener('orientationchange', doOnOrientationChange);
    
    0 讨论(0)
提交回复
热议问题