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

标签 xamarin xamarin.ios

我的 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/

相关文章:

c# - 如何更改 Xamarin.Droid 中选项卡式页面指示器的颜色?

ios - 表格 View 中单元格的动态高度不起作用

exception - Xamarin.Android 在异步方法中没有堆栈跟踪

c# - 如何在xamarin表单中实现圆形布局

android - 如何将 Material Design 标准图标导入 Visual Studio Xamarin 项目

c# - 如何从托管 (C#) 数组(例如 `NSArray` s)中创建 `int`?

c# - 尝试运行Monotouch-bindings示例Facebook时出错

iphone - 如何同时本地化 iPhone(monotouch) 和 Windows Phone 7 应用程序?

android - 进入应用前台调用特定代码

XCode 失去了从 xib 到 .h 文件的关联(MonoTouch)