编程语言
首页 > 编程语言> > c#-无法在Portrait,xamarin IOS中锁定一个视图控制器

c#-无法在Portrait,xamarin IOS中锁定一个视图控制器

作者:互联网

我正在尝试仅使一个视图控制器锁定为纵向模式,同时允许所有其他视图为任意方向.这就是我试图放入homeViewController(我想保持肖像的那个)的方式.

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations()
    {
        return UIInterfaceOrientationMask.Portrait;
    }
    public override UIInterfaceOrientation PreferredInterfaceOrientationForPresentation()
    {
        return UIInterfaceOrientation.Portrait;
    }
    public override bool ShouldAutorotate()
    {
        return false;
    }

我正在尝试在C#中的Xamarin中执行此操作.有人有建议吗?

解决方法:

在AppDelegate.cs中添加

    public bool RestrictRotation
    {
        get;
        set;
    }

并将其添加到同一AppDelegate.cs中,但在FinishedLaunching()方法之后

    [Export("application:supportedInterfaceOrientationsForWindow:")]
    public UIInterfaceOrientationMask  GetSupportedInterfaceOrientations(UIApplication application, IntPtr 
        forWindow)
    {
        if (this.RestrictRotation)
            return UIInterfaceOrientationMask.Portrait;
        else
            return UIInterfaceOrientationMask.All;
    }

在想要制作人像的每个ViewController中添加以下内容

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        this.RestrictRotation(true);
        // Perform any additional setup after loading the view, typically from a nib.
    }

    public override bool ShouldAutorotate()
    {
        return false;
    }

    public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations()
    {
        return UIInterfaceOrientationMask.Portrait;
    }

    public override UIInterfaceOrientation PreferredInterfaceOrientationForPresentation()
    {
        return UIInterfaceOrientation.Portrait;
    }

    void RestrictRotation(bool restriction)
    {
        AppDelegate app = (AppDelegate)UIApplication.SharedApplication.Delegate;
        app.RestrictRotation = restriction;
    }

标签:appdelegate,uiviewcontroller,xamarin,ios,c
来源: https://codeday.me/bug/20191118/2024603.html