Xamarin.ios 如何防止单个UIViewController以单触方式旋转?(所有其他视图都可以旋转)

Xamarin.ios 如何防止单个UIViewController以单触方式旋转?(所有其他视图都可以旋转),xamarin.ios,rotation,orientation,Xamarin.ios,Rotation,Orientation,我有一个iPhone应用程序,支持所有方向,但在一个UIViewController中,我只想支持纵向(或倒置) 我已将以下代码添加到UIViewController中,但它仍在旋转 public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation) { // Return true for supported ori

我有一个iPhone应用程序,支持所有方向,但在一个UIViewController中,我只想支持纵向(或倒置)

我已将以下代码添加到UIViewController中,但它仍在旋转

public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation     toInterfaceOrientation)
    {
        // Return true for supported orientations
        return ((toInterfaceOrientation != UIInterfaceOrientation.LandscapeLeft) && (toInterfaceOrientation != UIInterfaceOrientation.LandscapeRight));
    }
无论我在哪里添加ShouldAutorotateToInterfaceOrientation代码,它都会旋转

是否有方法允许应用程序支持除一个以外的所有UIViewController的所有方向


另外,我使用的是NavigationController-这会影响事情吗?

如果使用iOS 6,则必须覆盖GetSupportedInterfaceOrientations方法,而不是AutoRotateTointerFaceOrientation方法

    public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations()
    { ... }

这可能是因为根导航控制器带来了自己的旋转。让我们假设根导航控制器是普通的UINavigationViewController。创建自己的派生版本:

public class UINavigationControllerWithoutRotation : UINavigationController
{
    public UINavigationControllerWithoutRotation()
    {
    }

    public UINavigationControllerWithoutRotation(UIViewController viewController) : base(viewController)
    {
    }

    public override bool ShouldAutorotate()
    {
        return false;
    }
}
现在将其用作根控制器。找到这样的代码:

var myRootController = new UINavigationController();
并将其替换为

var myRootController = new UINavigationControllerWithoutRotation();

这应该可以完成任务。请记住,如果您的根视图控制器是一个UITabBarController,您必须做一些不同的事情

我添加了:public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(){return UIInterfaceOrientationMask.grait;}但它仍然会旋转!!