我的 Xamarin.iOS 应用程序在 iOS 16 之前运行良好。现在我的应用程序无法像以前那样更改方向。这之前是有效的:
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
最佳答案
这对我在所有版本的 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"));
}
您还需要在 AppDelegate 中管理 GetSupportedInterfaceOrientations
,以便它返回正确的方向。
关于xamarin - 如何在 iOS 16 中更改方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73992151/