简体   繁体   English

使WKWebView覆盖整个屏幕

[英]Make WKWebView cover the whole screen

I have a WKWebView that I want to cover the whole screen. 我有一个WKWebView,我想覆盖整个屏幕。 On newer devices that doesn't have a physical home button, iOS adds a white area under the "homebar". 在没有物理主页按钮的较新设备上,iOS在“主页”下方添加了一个白色区域。 I've tried to override the safeAreaInsets witch works fine to make the content to go under the "homebar", but id like to keep the top parameter to its default value. 我尝试覆盖safeAreaInsets巫婆,使其内容可以放在“ homebar”下,但id希望将top参数保持为其默认值。 This is since I have a navigation bar at the top witch can be hidden or shown. 这是因为我在顶部有一个导航栏,可以隐藏或显示女巫。 Setting UIEdgeInsets(top: 0... makes the WKWebView go behind my navbar (obviously). 设置UIEdgeInsets(top: 0...使WKWebView移到我的导航栏后面(显然)。

Is there anyway to only override left , bottom and right ? 无论如何,有没有只覆盖leftbottomright

import Foundation
import WebKit

class FullScreenWKWebView: WKWebView {
    override var safeAreaInsets: UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
}

Thanks! 谢谢!

Can't you solve this using autolayout, by pinning the webview to either the superview or the safe area, depending on what you want? 您能否通过自动布局来解决此问题,具体取决于您想要的是将Web视图固定到超级视图还是安全区域?

Anyway, you could probably do something like this: 无论如何,您可能会执行以下操作:

override var safeAreaInsets: UIEdgeInsets {
    let insets = super.safeAreaInsets
    return UIEdgeInsets(top: insets.top, left: 0, bottom: 0, right: 0)
}

I wouldn't recommend overriding the safeAreaInsets of any UIView directly as it might lead to unexpected behaviours as the one you mentioned yourself. 我不建议直接覆盖任何UIViewsafeAreaInsets ,因为它可能会导致意外的行为,就像您提到的那样。

These safeAreaInsets are generated on a higher UIViewController level, and therefore should be dealt there. 这些safeAreaInsets在更高的UIViewController级别上生成,因此应在此处处理。

I would rather advise doing as follows: 我宁愿建议如下:

Add this code to the UIViewController that will hold your FullScreenWKWebView . 将此代码添加到将保存FullScreenWKWebViewUIViewController中。

 override func viewWillLayoutSubviews() {
     super.viewWillLayoutSubviews()

     additionalSafeAreaInsets.bottom -= bottomLayoutGuide.length
 }

This way you will preserve the original UIViewController 's safeAreaInsets and simply add the desired modifier for the original. 这样,您将保留原始UIViewControllersafeAreaInsets并只需为原始对象添加所需的修饰符即可。 Allowing for modification at any stage. 允许在任何阶段进行修改。

So i figured it out, and boy, I was wrong. 所以我想通了,男孩,我错了。 Turns out it wasn't an issue with the constraints but how WKWebView render pages in safe areas. 事实证明,约束并不是问题,而是WKWebView如何在安全区域中呈现页面。 I figured out that my WKWebView covered the whole screen but didn't render the background under the home bar/notch. 我发现我的WKWebView覆盖了整个屏幕,但没有在主页栏/凹口下渲染背景。 The dark green color is defined in html. 深绿色在html中定义。

带有wkwebview的iPad无法覆盖整个屏幕

My solution to the problem was to disable WKWebViews bultin adjustment. 我对这个问题的解决方案是禁用WKWebViews公告调整。

webView.scrollView.contentInsetAdjustmentBehavior = .never

I also created two top constraint for my WKWebView with different priorities. 我还为WKWebView创建了两个具有不同优先级的顶级约束。 One to safeArea and one to superView. 一个到safeArea,一个到superView。 Then I change the priorities in code when the navbar is visible so that the page doesn't render under the navbar. 然后,当导航栏可见时,我会更改代码的优先级,以使页面不会在导航栏下方呈现。

Result: 结果: 结果无限制 With constraints to superView 受限于superView

条形结果 With constraints to safeArea 限制safeArea

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM