WKWebView does not display content correctly after changing orientation

I am new to iOS development and use WKWebViewto display a page that plays videos using Vimeo Player. When the application starts, it displays the view correctly, regardless of the orientation in which it is currently located. However, as soon as I change the orientation, the view will look enlarged or with spaces under the video.

Annoying that this is not appropriate. Sometimes the view is displayed correctly, sometimes not. If I try to enlarge or reduce the image or scroll it while using the application, it usually fixes, but even this does not seem to be 100% reliable.

A few screenshots (tested on iPad 2):

Landscape orientation (correct): Landscape orientation (correct)

Portrait orientation (correct):Portrait orientation (correct)

Landscape orientation (wrong): Landscape orientation (incorrect)

Portrait orientation (wrong): Portrait orientation (incorrect)

And the code used to get this result:

import Foundation
import UIKit
import WebKit

class VideoViewController : UIViewController, WKScriptMessageHandler {

@IBOutlet var containerView : UIView! = nil
var webView: WKWebView?

override func loadView() {
    super.loadView()
    self.webView = WKWebView()
    let contentController = WKUserContentController();
    let scaleToFit = WKUserScript(source: "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width, initial-scale=1.0'); document.getElementsByTagName('head')[0].appendChild(meta);", injectionTime: WKUserScriptInjectionTime.AtDocumentStart, forMainFrameOnly: true)
    contentController.addUserScript(scaleToFit)
    contentController.addScriptMessageHandler(self, name: "callbackHandler")
    self.view = webView!
}

override func viewDidLoad() {
    super.viewDidLoad()
    refreshUI()
}

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

func refreshUI() {
    let url = NSURL(string: "https://photofactsacademy.nl/api/vp.asp?id=152839850")
    let requestObj = NSURLRequest(URL: url!)
    webView!.loadRequest(requestObj)
}

func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
    if(message.name == "callbackHandler") {
        print("JavaScript is sending a message \(message.body)")
    }
}
}

I looked through SO for WKWebView and orientation changes and did not find anything that helped me.

Your help is appreciated.

+5
source share
4 answers

Can you try this

wkWebView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
+11
source

I was able to solve this problem by entering a javascript reload request as follows:

Swift:

 func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animateAlongsideTransition(nil, completion: {
     //Reset Frame of Webview
     webView.evaluateJavaScript("location.reload();", completionHandler: nil)
}

Goal C:

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    //Reset Frame of Webview
    [_webView evaluateJavaScript:@"location.reload();" completionHandler:^(id _Nullable obj, NSError * _Nullable error) {
        NSLog(@"error:%@", error);
    }];
}
+3
source

:

<header><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, shrink-to-fit=no'></header><body style='margin: 0px; padding: 0px; width:100%; height:100%;'>...

Set the width and height of the body and your video element to 100% and disable user scaling using the title tag.

0
source

You do not need to reload the entire web page. You just need to reload its input views. Make sure you set the limits for web browsing correctly, and then add the code below when changing orientation.

   // Reload inputs so that webview can adjust its content according to orientation
     [self.webView reloadInputViews];
0
source

Source: https://habr.com/ru/post/1626217/


All Articles