首页 > 解决方案 > iOS 上的全屏 web 视图

问题描述

这显然是一件很容易做到的事情,但即使经过几天的敲打,我也无法做到。

我需要的是,当在 webview 上按下按钮时,我想让我的 webview 全屏显示。我需要隐藏状态栏和底栏才能完成全屏显示。状态栏区域的内容应该是可点击的。我的 webview 在 mainviewcontroller 上。我使用的是 XIB 而不是情节提要或 swiftUI。

我尝试过的是,我使用隐藏状态栏

UIApplication.shared.setStatusBarHidden(enable, with: UIStatusBarAnimation.slide)

我已经使用

    func viewInset() -> (CGFloat, CGFloat) {
        if #available(iOS 11.0, *) {
            let insets = UIApplication.shared.keyWindow?.safeAreaInsets ?? .zero
            return (max(insets.top, 20), max(insets.bottom, 20))
        }
        return (20.0, 20.0)
    }
    
    func enableFullScreen(enable: Bool) {
        DispatchQueue.main.async {
            let (topInset, bottomInset) = self.viewInset()
            self.topConstraint.constant = enable ? -topInset : 0
            self.bottomConstraint.constant = enable ? -bottomInset : 0
            var contentInset = self.webView.scrollView.contentInset
            contentInset.top = contentInset.top - topInset
            contentInset.bottom = contentInset.bottom - bottomInset
            self.webView.scrollView.contentInset = contentInset
            UIApplication.shared.setStatusBarHidden(enable, with: UIStatusBarAnimation.slide)
        }
    }

我最终得到的是以下 -

红色边框是 webview。深蓝色/黑蓝色是我的内容。我的 webview 将背景颜色设置为绿色,如下图所示。

问题- 我无法让 webview 内容占据全屏,即包括绿色区域。绿色空间是 webview,因为我的背景颜色是绿色。即使我重新加载整个内容,内容也不会显示在那里。位于状态栏区域的任何内容都不可点击。

必须有更好的方法来全屏显示。有人可以建议吗?

请不要删除问题。让我知道如果没有提供任何细节,我会做的。

标签: iosswiftwebviewfullscreen

解决方案


推荐阅读