I added a WKWebView subview into a PhoneGap / Cordova 4.0+ app (via cordova-plugin-wkwebview-engine) and used programmatic AutoLayout constraints to get the WKWebView to size the same as the MainViewController's view.
When I view the bounds of the WKWebView, it matches the MainViewController view's bounds.
But using Javascript to inspect the content size, specifically script that looks like:
var body = document.body;
var html = document.documentElement;
Math.max(body.scrollHeight,body.offsetHeight,html.clientHeight,html.offsetHeight);
This returns a height that's actually about 1/2 to 2/3rds of the WKWebView bounds height (on an iPad Pro simulator I'm setting bounds height to 1024.0 but the Javascript reports a content height of only 735.0). And I'm only displaying half of the non-resizing Vue.js content I'd love to display.
Since I can't show the actual content I'm working with (NDA fun!), I actually noticed the problem happens with any URL I pass into WKWebView within PhoneGap/Cordova. Here is what apple.com looks like in my app:
And here is what it should look like, the correctly sized version, in Mobile Safari (running in the simulator):
You can see the top bar items are differently spaced (on the desktop they're supposed to be indented from either side of the window somewhat) and there's more revealed in the bottom edge, because the web view's internal height is taller.
To fix my problem, I've done everything from move the WKWebView instantiation from viewDidLoad
out to viewDidAppear
, and also tried "hard coding" a static iPad Pro-sized framesize being passed into [WKWebView initWithFrame:]
, but content size remains at 735.0. This same content renders beautifully (and correctly) both in the Simulator's Mobile Safari and in Desktop Safari.
How can I force WKWebView's content size to match the bounds of the parent view?
If you'd like to see this bug in action on the iPad Pro simulator, I have a public github repo at https://github.com/dautermann/phonegap-air. Be sure to fetch the "demonstrate-issue" branch.