window.innerWidth/Height not updated in resize handler in WKWebView

南笙酒味 提交于 2020-01-24 09:14:28

问题


I have some fullscreen web content that I update when the window changes size using the resize event.

window.onresize = function()
{
    var width = window.innerWidth;
    var height = window.innerHeight;
    ...
}

This works fine in the Safari app but it is not working in a WKWebView. It is giving me the wrong window dimensions when the device changes screen orientations on both iPhone and iPad in a WKWebView.

Specifically for iPad I am showing the web view in a navigation controller. When I switch to portrait mode I get (768x768) dimensions instead of (768x960) which are the correct dimensions.

Is there a workaround to this issue?


回答1:


It seems like window.clientWidth / window.clientHeight don't get updated before the resize event fires.

I found two solutions to the issue:

  • use document.documentElement.clientWidth instead of window.innerWidth
  • listen to the orientationchange event instead of the resize event (window.clientWidth seems to be already updated at this point)


来源:https://stackoverflow.com/questions/43302314/window-innerwidth-height-not-updated-in-resize-handler-in-wkwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!