【问题标题】:How to change orientation in iOS 16?如何在 iOS 16 中更改方向?
【发布时间】:2022-10-08 04:07:10
【问题描述】:

在 iOS 16 之前,我的 Xamarin.iOS 应用程序运行良好。现在我的应用程序无法像以前那样更改方向。这是以前的工作:

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

【问题讨论】:

    标签: xamarin xamarin.ios


    【解决方案1】:

    这适用于所有版本的 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 以便它返回正确的方向。

    【讨论】:

      猜你喜欢
      • 2022-12-15
      • 2022-09-27
      • 2022-10-26
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多