wkwebview在横向模式下无法正常显示

时间:2018-03-24 15:30:13

标签: ios swift swift4 wkwebview landscape-portrait

这就是我的故事板看起来的样子

enter image description here

以下是我的代码

class ViewController: UIViewController,WKNavigationDelegate {//WKUIDelegate

@IBOutlet weak var webContainer: UIView!
var webView : WKWebView!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    initMyWeb()
}

func initMyWeb() -> Void {


    let myBlog = "https://google.com"
    let myURL = URL(string: myBlog)
    let request = URLRequest(url: myURL!)

    // init and load request in webview.
    webView = WKWebView(frame: self.webContainer.frame)
    webView.navigationDelegate = self
    webView.load(request)
    self.webContainer.addSubview(webView)
    self.webContainer.sendSubview(toBack: webView)
}


//MARK:- WKNavigationDelegate

func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
    print(error.localizedDescription)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
    print("Start to load")
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    print("finish to load")
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    webView.frame = self.webContainer.frame
}

当我在横向模式下运行时,它看起来像这样

enter image description here

我的问题是:如何让它在横向模式下适应整个屏幕?我给它的大小等于 webContainer 视图

1 个答案:

答案 0 :(得分:0)

试试这个

webView = WKWebView(frame: self.webContainer.frame)
webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
webView.navigationDelegate = self
webView.load(request)