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()
{
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?