为什么以下不起作用?目的是让 div
的宽度为 iPhone 屏幕的 1/2。它在桌面浏览器(例如 OSX Safari 和 Chrome)中按预期工作。
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 模拟器:
最佳答案
似乎有元视口(viewport)
bugs在 xcode 7.0.1 + iOS9 中;但是,initial-scale=1.0001
和 shrink-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>
关于ios - 元视口(viewport)标签在 iOS UIWebView 中似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33103791/