如何使 WKWebView 像 SFSafariViewController 一样全屏显示?

How do I make WKWebView full screen like the SFSafariViewController?

我有一个包含 Web url 列表的 TableView。我想打开一个导航到所选 url 的浏览器,但具有一些自定义功能。当我使用 SFSafariViewController 时,我得到了全屏浏览器。但是,当我使用 WKWebViewController 时,我得到了您向下滑动即可关闭的模式。

如何让 WKWebView 显示全屏,而不是像模态一样?

使用SFSafariViewController

class ShoppingViewController: UITableViewController {

    ...

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        showRetailerHome(indexPath: indexPath)
    }

    func showHome(indexPath: IndexPath){

        ...

        if let url = URL(string: urlString){
            let config = SFSafariViewController.Configuration()
            let vc = SFSafariViewController(url: url, configuration: config)
            present(vc, animated: true)
        }        
    }

结果:

使用WKWebViewController

class ShoppingViewController: UITableViewController {

    ...

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        showRetailerHome(indexPath: indexPath)
    }

    func showHome(indexPath: IndexPath){

        let customWebController = WKBrowserController()
        present(customWebController, animated: true)

    }

WKBrowserController:

class WKBrowserController: UIViewController, WKNavigationDelegate {

    var webView: WKWebView!

    override func loadView() {
        webView = WKWebView()
        webView.navigationDelegate = self
        self.view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let url = URL(string: "https://www.amazon.com")!
        webView.load(URLRequest(url: url))
        webView.allowsBackForwardNavigationGestures = true
    }
}

结果:

class WKBrowserController: UIViewController, WKNavigationDelegate {

    var Activity: UIActivityIndicatorView!

    var webView = WKWebView()

    override func viewDidLoad() {
        super.viewDidLoad()
        Activity = UIActivityIndicatorView()
        Activity.center = view.center
        view.addSubview(Activity)

        guard let url = URL(string: "your-url-string") else { return }
        webView = WKWebView(frame: self.view.frame)
        webView.translatesAutoresizingMaskIntoConstraints = false
        webView.isUserInteractionEnabled = true
        webView.navigationDelegate = self
        view.addSubview(webView)
        let request = URLRequest(url: url)
        webView.load(request)

        webView.addSubview(Activity)
        Activity.startAnimating()
        webView.navigationDelegate = self
        Activity.hidesWhenStopped = true
    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if navigationAction.navigationType == .linkActivated  {
            if let url = navigationAction.request.url,
                let host = url.host, !host.hasPrefix("your-url-string"),
                UIApplication.shared.canOpenURL(url) {
                UIApplication.shared.open(url)
                print(url)
                print("Redirected to browser. No need to open it locally")
                decisionHandler(.cancel)
            } else {
                //print("Open it locally")
                decisionHandler(.allow)
            }
        } else {
            //print("not a user click")
            decisionHandler(.allow)
        }
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        Activity.stopAnimating()
    }

    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        Activity.stopAnimating()
    }
}

当您全屏显示 viewcontroller 时。你可以设置为.fullscreen

尝试:

let customWebController = WKBrowserController()
customWebController.modalPresentationStyle = .fullScreen
present(customWebController, animated: true)