简体   繁体   中英

swift iOS 11.2 WKWebView doesn't show up

I'm trying to implement a simple web browser in my application. Because I need to control cookies, I decided to go with WKWebView.

The official example works well, but that example 1) doesn't use storyboard 2) sets view to webview directly

So I added my WKWeView to storyboard and connected to an outlet, then loads an URL in viewDidLoad :

class BrowserViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {

    @IBOutlet var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        print("browser view loaded")
        webView.uiDelegate = self
        webView.navigationDelegate = self

        let myURL = URL(string: "https://www.youku.com")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)

    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

My Storyboard:

故事板

However, when I run my app in the simulator, the WKWebView does not appear:

在此处输入图片说明

How can I fix this problem? Thanks.


UPDATE:

This is the layout and constraints of the view

在此处输入图片说明

UPDATE:

So I removed all the unnecessary constraints and the layout seems to be fixed. However, the web view is still blank.

If I add a line view = webView at the end of viewDidLoad . The web page renders correctly.

(For some reason the images would upload, I'll try again later)

UPDATE:

Unfortunately, move everything to viewWillAppear doesn't work either.

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        print("browser view loaded")

        webView.uiDelegate = self
        webView.navigationDelegate = self

        let myURL = URL(string: "https://www.bing.com")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }

Webview is still blank.

Delete IBOutlet and try connect to an outlet again.

May be

import UIKit
import WebKit

class ViewController: UIViewController {
    @IBOutlet weak var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        print("browser view loaded")
        webView.uiDelegate = self
        webView.navigationDelegate = self

        let myURL = URL(string: "https://www.youku.com")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }
}

extension ViewController: WKUIDelegate {

}

extension ViewController: WKNavigationDelegate {

}

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

I think your issue is related to the StackView options. Please show also what settings did you set to your StackView? Alignment and Distribution are the main things.

Try setting:

Alignment -> Center Distribution -> Fill

Remove all constraints from Toolbar and WebView, and leave only the Toolbar Height = 56. That is the only constraint needed inside the StackView. Also, make sure you leave the StackView's constraints, as those define the constraints for the objects contained inside. Make sure there are no red borders around your views (if it is red, then you're missing something).

I found out how to make it work, but the reason behind it remains a mystery.

Apparently, if I move the WKWebView out of the stack view, it renders the web page just fine.

布局

屏幕

PS the url is changed to https://www.bing.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM