iosxamarin.formsxamarin.iosscreen-rotation

How to change orientation of some pages in iOS 16 in Xamarin Forms?


I want to change orientation of some pages in my app in Xamarin Forms.

I am using Visual Studio 2019 for Mac and Xcode version 12.4

I have used DependencyService. It is working fine till iOS 15 but not able to rotate in iOS 16.

I am sharing sample code for reference.

AppDelegate.cs

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, [Transient] UIWindow forWindow)
    {
        if (App.IsAllowRotation)
        {
            return UIInterfaceOrientationMask.Landscape;
        }

        else
        {
            return UIInterfaceOrientationMask.Portrait;
        }
    }

Service in iOS

public class OrientationService : IOrientationHandler
{

    public void Landscape()
    {
        UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeRight), new NSString("orientation"));
    }

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

Till iOS 15 it is working fine. For iOS 16, I am trying to update Service by adding below code:

public class OrientationService : IOrientationHandler
{

    public void Landscape()
    {

        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)
                {                        
                    nav.SetNeedsUpdateOfSupportedInterfaceOrientations();

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

    public void Portrait()
    {
        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"));
        }
    }
 }

I am getting errors for methods - SetNeedsUpdateOfSupportedInterfaceOrientations(), RequestGeometryUpdate, UIWindowSceneGeometryPreferencesIOS

May I know any namespace is required?

What changes need to be done for iOS 16 in Service and AppDelegate in Xamarin Forms iOS app?


Solution

  • These methods SetNeedsUpdateOfSupportedInterfaceOrientations(), RequestGeometryUpdate, UIWindowSceneGeometryPreferencesIOS are newly added methods in UIKit in iOS16 Release Note. I tried it in XCode14.1 and VS2022 and there is no error. According to the XCode release version, XCode14.0 starts to support iOS16. It is recommended that you update the XCode version to the latest version and try again.