Home > Software engineering >  How to change orientation in iOS 16?
How to change orientation in iOS 16?

Time:10-10

My Xamarin.iOS app was working just fine until iOS 16. Now my app can not change orientations like it used to. This was working before:

UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));

CodePudding user response:

This works for me on all versions of iOS:

if (UIDevice.CurrentDevice.CheckSystemVersion(16, 0))
{
    var windowScene = (UIApplication.SharedApplication.ConnectedScenes.ToArray()[0] as UIWindowScene);
    if (windowScene != null)
    {
        var nav = UIApplication.SharedApplication.KeyWindow?.RootViewController;
        if (nav != null)
        {
            // Tell the os that we changed orientations so it knows to call GetSupportedInterfaceOrientations again
            nav.SetNeedsUpdateOfSupportedInterfaceOrientations();

            windowScene.RequestGeometryUpdate(
                new UIWindowSceneGeometryPreferencesIOS(UIInterfaceOrientationMask.Portrait),
                error => { }
            );
        }
    }
}
else
{
    UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
}

You need to also manage GetSupportedInterfaceOrientations in your AppDelegate so that it returns the correct orientation.

  • Related