使WKWebview成为“真实"商品在iPhone X上全屏显示(从WKWebView删除安全区域 [英] make WKWebview "real" fullscreen on iPhone X (remove safe area from WKWebView

查看:575
本文介绍了使WKWebview成为“真实"商品在iPhone X上全屏显示(从WKWebView删除安全区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在App WebView中实现真正的"全屏显示,这意味着无论任何内容,我都希望Webcontent完全处于陷井之下.

I'm trying to implement a "real" fullscreen in App WebView, meaning I want the webcontent to fully go under the notch no matter what.

这是将WebView框架化为超级视图边界时所面临的当前情况: 红色边框是webView的边框(也就是屏幕的大小). Twitter的身高和宽度均为100%.

This is the current situation I am facing, when framing the WebView to the superviews bounds: The red border is the border of the webView (Also the size of the screen). Twitter has 100% height and width on the body.

我了解网站在Safari中的宽度不能为100%,并且它是应用内浏览器遵守安全区的默认行为,但特别是在我的情况下,因为某些网页是针对应用设计的,所以我需要使用全部空间.

I understand that websites can't have 100% width in Safari and that its the default behaviour for the in App Browser to respect the safearea but especially in my case, where the webpages are somewhat designed for the app, I need to use the full space.

我不知道如何设置网络视图以使其内容具有完整尺寸.有没有办法改变安全区?

I couldn't figure out how to set the webview to give its content the full size. Is there a way to change the safearea?

代码段:

private var webView : WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.webView = WKWebView(frame: .zero)

    self.webView.layer.borderColor = UIColor.red.cgColor
    self.webView.layer.borderWidth = 2

    self.webView.load(URLRequest(url: URL(string: "https://www.twitter.com")!))

    self.view.addSubview(self.webView)
}

override func viewDidLayoutSubviews() {
    self.webView.frame = self.view.bounds
}

推荐答案

经过一番尝试和错误,这是我想到的解决方案. 子类WKWebView并创建一个自定义类.覆盖safeAreaInsets:

After a little try and error, this is the solution I came up with. Subclass WKWebView and create a custom class. Overwrite safeAreaInsets:

import Foundation
import WebKit

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

这篇关于使WKWebview成为“真实"商品在iPhone X上全屏显示(从WKWebView删除安全区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆