JiaKangZhong China
JiaKangZhong China

Reputation: 21

When WKWebView displays content, it will be white for about 1~2 second

When WKWebView displays content, it will be white for about 1~2 second. Not a memory problem, I use [self loadHTMLString:string baseURL:baseURL]; show the HTML content, but each time need to wait 1~2 second will be normal display. UIWebview does not have this problem.

Is there any way to solve it? Or optimize this blank time?

Upvotes: 2

Views: 531

Answers (2)

Ely
Ely

Reputation: 9131

Most effective solution on iOS to prevent unwanted white background for WKWebView, in case the rest of your view setup is dark:

webView.isOpaque = false

Upvotes: 1

Mozahler
Mozahler

Reputation: 5303

Sometimes it takes a while to load a page over the network. The page is white until the data can be loaded. A simple solution is to issue your load() command early. Before it needs to be called. I anticipate the next page, but don't display it until it is needed. When it is needed, it is already loaded, I just need to add it:

viewController.view.addSubview(webView)

I use two different methods, depending on my needs: I either load the view early and change the alpha to 0 so it is invisible:

webView.alpha = 0

Of course, I change it to 1.0 when I want it to display.

Or I remove the web view from the superview (it needs to be a class property so it is retained), like this:

func removeWebView(_: UITapGestureRecognizer) {
    print("removewebview tap")
    webView.removeFromSuperview()
}

If you follow this approach, you will need to call a method that adds the subview (web view) back, and if you are using auto layout, you need to reapply constraints because they are lost when you remove the web view from the superview. If you do that, don't forget to call .setNeedsLayout() on the parent view.

Upvotes: 1

Related Questions