2015-10-13 52 views
0

为什么不是以下工作?目的是让div拥有iphone屏幕宽度的1/2。它在桌面浏览器(如OSX Safari和Chrome)中按预期工作。元视口标签似乎在iOS中不起作用UIWebView

的index.html:

<html> 
<head>   
    <meta name="viewport" content="width=device-width,initial-scale=1" /> 
</head> 
<body> 
<br><br><br> 
    <div style="background-color: red; width: 50%">hello iphone</div> 
</body></html> 

iOS的代码:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"]; 
    [_webView loadRequest:[NSURLRequest requestWithURL:url]]; 
} 

iphone模拟器:

iphone 4s simulator

回答

0

看来在xcode 7.0.1 + iOS9中有meta viewportbugs;但是,建议的解决方法如initial-scale=1.0001shrink-to-fit=no尚未适用于我。我已经尝试过模拟器和物理设备。

这只是到目前为止,已经工作的解决方案:

<html> 
    <body> 
     <br><br><br> 
     <div id="red-div" style="background-color: red;">hello iphone</div> 

     <script> 
      document.getElementById('red-div').style.width = screen.width/2 + 'px'; 
     </script> 

    </body> 
</html>