我发现如果我使用 WKWebView 与
viewport-fit=cover
并且。body :{height:100%}
HTML页面的高度仍无法达到iPhone X底部,与安全区域的高度相等。然而,背景颜色可以覆盖全屏。
https://ue.qzone.qq.com/touch/proj-qzone-app/test.html
我在全屏WKWebView中加载此页面以重现问题。
我已经修复了(ObjC / Swift)的问题:
if (@available(iOS 11.0, *)) {
webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
或者if #available(iOS 11.0, *) {
webView.scrollView.contentInsetAdjustmentBehavior = .never;
}
这个设置似乎与viewport-fit=cover
具有相同的效果,因此如果您知道您的内容正在使用该属性,则可以通过这种方式修复错误。
env(safe-area-inset-top)
CSS 声明仍按预期工作。WKWebView 自动检测其视口是否与被阻止的区域相交,并相应地设置值。
contentInsetAdjustmentBehavior和其参数值的文档,感谢@dpogue提供了答案,我在那里找到了解决方案。
<preference>
)来控制它。 - Doctor.Who.我发现在CSS中将html
元素的高度设置为height: 100vh
(而不是height: 100%
)起作用。
opacity: 0.5;
到 html 和 body 标签中,你会发现 body 标签占据整个屏幕,而 html 标签的高度只有安全区域那么高。<html style='height: 812px;'>
。只要你还添加了 <meta name="viewport" content="initial-scale=1.0, viewport-fit=cover">
,这将使 html 内容适合整个屏幕。我在我的 Cordova 应用程序中遇到了这个问题。
Samantha 的解决方案在某种程度上对我有用,但在横向和其他设备上设置为 812px 的 html 标签高度会导致问题。最终,我发现仅针对 iPhone X 大小的屏幕使用 css 媒体查询来处理横向和纵向问题是行之有效的。
必须将宽度和高度像素值声明为重要,以便 iPhone 接受它们。
@media only screen
and (device-width : 375px)
and (device-height : 812px)
and (-webkit-device-pixel-ratio : 3)
and (orientation : portrait) {
html {
height: 812px !important;
width: 375px !important;
}
}
@media only screen
and (device-width : 375px)
and (device-height : 812px)
and (-webkit-device-pixel-ratio : 3)
and (orientation : landscape) {
html {
width: 812px !important;
height: 375px !important;
}
}
safe-area-inset-top
。因此,在任何iOS 11设备上,您都会在底部获得20像素的间隙。这在iOS 10中不是问题。 - Sampoviewport-fit=cover
或其他一些东西。 - Sampo