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

问题描述 投票:3回答:2

我试图只允许一个视图控制器锁定在纵向,同时允许所有其他视图是任何方向。这就是我试图放入我的homeViewController(我想保留的肖像)。

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

我试图在c#中的xamarin中这样做。有人有什么建议吗?

c# ios xamarin uiviewcontroller appdelegate
2个回答
2
投票

在AppDelegate.cs中添加它

    public bool RestrictRotation
    {
        get;
        set;
    }

并在FinishedLaunching()方法之后在相同的AppDelegate.cs中添加它

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

将以下内容添加到您要创建Portrait的每个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;
    }

1
投票

我从一个基地派生我的控制器。我需要这个用于其他目的,但也用于锁定纵向方向

public class BaseView : UIViewController
{

    static List<Type> SupportingLandscapeScreenTypes = new List<Type>()
    {
        typeof(TemperaturesHistoryView), 
        typeof(LoadSwitchConsumptionView), 
        typeof(HomeConsumptionGraphView)

    };

    public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations()
    {
        foreach (var screenType in SupportingLandscapeScreenTypes)
        {
            if (GetType() == screenType)
                return UIInterfaceOrientationMask.Portrait | UIInterfaceOrientationMask.LandscapeLeft | UIInterfaceOrientationMask.LandscapeRight;
        }
        return UIInterfaceOrientationMask.Portrait;
    }

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

}

public class MyEnergateAppNavigationController:UINavigationController
{
    public MyEnergateAppNavigationController(UIViewController rootController)
        :base (rootController)
    {
    }

    public override bool ShouldAutorotate()
    {
        return true;
    }

    //[Obsolete ("Deprecated in iOS6. Replace it with both GetSupportedInterfaceOrientations and PreferredInterfaceOrientationForPresentation")]
    //public override bool ShouldAutorotateToInterfaceOrientation(UIInterfaceOrientation toInterfaceOrientation)
    //{
    //    return TopViewController.ShouldAutorotateToInterfaceOrientation(toInterfaceOrientation);
    //}

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

    public override UIInterfaceOrientation PreferredInterfaceOrientationForPresentation()
    {
        return TopViewController.PreferredInterfaceOrientationForPresentation();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.