WKWebView displays content wrong after orientation change

后端 未结 4 1889
温柔的废话
温柔的废话 2020-12-30 09:43

I am fairly new to iOS development and I\'m using a WKWebView to display a page that plays a video using the Vimeo Player. When the app starts up, it shows the

相关标签:
4条回答
  • 2020-12-30 10:11

    Can you try this

    wkWebView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    
    0 讨论(0)
  • 2020-12-30 10:15

    I was able to solve this problem by injecting a javascript reload call like this:

    Swift:

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

    Objective 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);
        }];
    }
    
    0 讨论(0)
  • 2020-12-30 10:27

    You don't need to reload the entire web page. You just need to reload its input views. Make sure you have set constraints to web view properly and then add below code while changing orientation.

       // Reload inputs so that webview can adjust its content according to orientation
         [self.webView reloadInputViews];
    
    0 讨论(0)
  • 2020-12-30 10:29

    After few hours of struggling, this is what worked for me:

    <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 width and height of both body and your video element to 100% and disable user scaling through header tag.

    0 讨论(0)
提交回复
热议问题