GameWindow orientation different between android and ios


So I’m probably doing something wrong… again. But maybe someone can explain it to me.

On Android:

  1. You change your orientation of your device
  2. GameWindow.OrientationChanged is raised
  3. Your android device flipps/changes resolution
  4. GameWindow.ClientSizeChanged is raised

On iOS:

  1. You change your orientation of your device
  2. Your ios device flipps/changes resolution
  3. GameWindow.OrientationChanged is raised
  4. GameWindow.ClientSizeChanged is never raised.

I was using the ClientSizeChanged to resize my RenderTarget if the resolution changed. I do this as the android device hasn’t updated the display mode before rasing the OrientationChanged. Why is that?

On iOS the ClientSizeChanged is never raised but OrientationChanged is raised after the device have updated display mode. Why is ClientSizeChanged never raised?

My hack is easy I just check if I’m on a ios device then I use OrientationChanged else I use ClientSizeChanged. This works in all the scenarios I have encountered so far. But maybe there is an obvious fix I’m missing?

This is a method that might work for you, I picked it up previously from elsewhere on the forum and integrated it into my project. I can’t test on iOS but it works for screen rotation on UWP and Android.

Add this to the end of your Initialize():
Window.ClientSizeChanged += Window_ClientSizeChanged; //fire your own method when screen is resized
Window.OrientationChanged+= Window_ClientSizeChanged; //also fire your method when screen is rotated
Window_ClientSizeChanged(graphics, null); //launch your method for the first time to get things started (make sure yourWidth and yourHeight have valid values!)
Then end it with base.Initialize(), as you normally would.

The keys of your new method Window_ClientSizeChanged will be the graphics.ApplyChanges() call and your own resolution values for the new width and height. For simplicity I’ve called them yourWidth and yourHeight:

protected void Window_ClientSizeChanged (object sender, EventArgs args)
graphics.ApplyChanges(); //saves graphics changes, needs to go before the following lines currently

graphics.PreferredBackBufferWidth = yourWidth; //changes window shape to match new screen resolution/orientation
graphics.PreferredBackBufferHeight = yourHeight; //these need to go after the ApplyChanges call to display the window bar, especially on Mac, though this necessitates calling this method twice

This may not be the most optimized solution, but I ran into bugs when I switched ApplyChanges() and the Preferred lines. If you’re calling Window_ClientSizeChanged in Update(), call it twice in a row to make sure the new resolution is applied. For orientation changes, it should work automatically.

Please let me know if this works on iOS for you. I may improve my own code if it doesn’t.