As far as I know, resize event does not necessarily fire on orientation change. This is device/browser specific? I wanted to suggest the orientationchange Event, but as stated in MDN it is depricated already. I cannot find any alternative event for that matter.
Is the resize event the new standard for both resize and orientation change? Does someone know?
Update: I just tested the resize event on my Iphone 6 and it does fire on orientation change. Did you check if it is firing for you at all?
To really detect orientation change and also portrait and landscape, you can write a function like this, that is fired inside the resize event:
function checkOrientation() {
if(window.innerWidth > window.innerHeight) return 'landscape'
else return 'portrait'
}
let orientation: string
function onresize() {
if(orientation !== checkOrientation()) {
// changed
orientation = checkOrientation()
}
}
I believe sometimes there is a delay between the orientation change and the height/width values updating so maybe try using setTimeout to delay your OnContextResized()?
Aaah I spent ages on this issue a month or so back.
There are two solutions, one kludge and one OK solution.
The kludge is as the above comment says to detect the window change as normal and if its IOS Safari set a timeout for 100 milis before executing the code that asks the window/containers size. For whatever reason the size updates are delayed on ios safari.
The more elegant solution is to instead of listening to resize events or orientation change events, use the resizeobserver api (look it up on mdn Web docs, also note compatibility on caniuse) this will give you the correct dimensions for all devices (that support the api). I’m on my phone so can’t give code - let us know how that goes.
This is caused by an iOS bug to my knowledge. You can use setTimeout(()=>{},N) to set an N delay before executing any code that depends on innerWidth and innerHeight values as a workaround. I needed to set a delay of at least 55 milliseconds when testing on my local machine for instance.