使用WKWebView时,旋转时Youtube视频布局损坏

时间:2019-05-29 05:21:38

标签: ios wkwebview

我正在使用WkWebView在YouTube上播放video。但是当我旋转设备时,video layout坏了。我已经在Google搜索解决方案,但仍然没有成功。我尝试使用autoresizingMask,但也无法使用。

如果有人可以帮助我,我真的很感激。

My Demo

这是我的代码:

    import UIKit
    import WebKit

    class ViewController: UIViewController, WKNavigationDelegate, UIScrollViewDelegate {

        var webview: WKWebView!
        @IBOutlet weak var containerView: UIView!

        override func viewDidLoad() {
            super.viewDidLoad()

            var config = WKWebViewConfiguration()
            config.allowsInlineMediaPlayback = true

            webview = WKWebView(frame: view.frame, configuration: config)
            webview.translatesAutoresizingMaskIntoConstraints = false
            webview.autoresizingMask = [.flexibleHeight, .flexibleWidth]
            view.addSubview(webview)

            //Constraint webview
            webview.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
            webview.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
            webview.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
            webview.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

            webview.navigationDelegate = self
            webview.scrollView.delegate = self
            webview.load(URLRequest(url: URL(string: "https://m.youtube.com/watch?v=zP3cTWbYWYU")!))
        }
        func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
            print(#function)
        }

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

        func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
            decisionHandler(.allow)
        }

    }

0 个答案:

没有答案