Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView content jumping on load

I've created a simple test app to reproduce my problem reliably and minimize the moving parts. It contains just a navigation controller and a view controller that I add a WKWebView to. I immediately tell that WKWebView to navigate to www.google.ca. Once the page has loaded, the content automatically jumps up underneath the header bar. This is reproducible consistently on various simulators/devices running iOS 8/9/9.1. Below is the code for my ViewController:

//  ViewController.swift
import UIKit
import WebKit

class ViewController: UIViewController {
    let webView = WKWebView()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(webView)
        let url = NSURL(string: "http://www.google.ca")!
        let request = NSURLRequest(URL: url)
        webView.loadRequest(request)
        webView.frame = view.bounds
    }
}

GIF

like image 206
jvoll Avatar asked Oct 30 '15 22:10

jvoll


People also ask

How to load a file from a URL in wkwebview?

Once we have the local web file URL we can use the loadFileURL method on WKWebView to load the file from the url that we pass to it. We pass the same url to allowReadAccessTo.

What is the use of the file argument in a WebView?

This argument will allow the web view to only open that specific file. If we passed a directory as an argument, the web view would only be able to load a file within that directory.

How does WebView work with Interface Builder?

Webview works easily with interface builder, just a single line of code assigning the view to the webview, in iOS or macOS. Our app features context sensitive help so that the appropriate HTML displays in the view the user is in. There are over 80 HTML files so asking the user to exit the app and open another window, etc.


1 Answers

put webView.frame = view.bounds before view.addSubview(webView)

like image 169
tsing gao Avatar answered Oct 17 '22 04:10

tsing gao