xamarin 如何在iOS 16中更改方向?

uyto3xhc  于 2022-12-07  发布在  iOS
关注(0)|答案(1)|浏览(868)

我的Xamarin.iOS应用程序在iOS16之前运行得很好。现在我的应用程序不能像以前那样改变方向了。以前是这样的:

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

xbp102n01#

这对我在所有版本的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,以便它返回正确的方向。

相关问题