【问题标题】:Can't lock one view controller in Portrait , xamarin IOS无法在 Portrait , xamarin IOS 中锁定一个视图控制器
【发布时间】:2017-03-24 19:12:24
【问题描述】:

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

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

我正在尝试在 c# 中的 xamarin 中执行此操作。有人有什么建议吗?

【问题讨论】:

  • 您的代码适合UIViewController,即PresentViewControllering 另一个UIViewController....但是您使用的是UINavigationController吗?和/或您的homeViewController 是第一个在应用启动时显示的 VC(?)并且必须是纵向的并且之后的所有 VC 都已解锁?
  • 是的,我正在使用 UINavigationController。 homeViewController 是第一个加载的。我只想将视图锁定为纵向,其余视图锁定在任何方向。

标签: c# ios xamarin uiviewcontroller appdelegate


【解决方案1】:

在 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;
    }

将以下内容添加到您希望制作肖像的每个视图控制器

    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) 在这些 Portrait VC 中,添加 ViewWillDisappear 即可完成 app.RestrictRotation = false;。否则,您的应用可能会卡在纵向。 2)使用ViewWillAppear而不是ViewDidLoad;否则返回(弹出)到现有视图将不会受到限制。
【解决方案2】:

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

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();
    }
}

【讨论】:

  • 如果您愿意,也可以在 GetSupportedInterfaceOrientations 中使用 LINQ
  • 所以这是一个完全不同的类?只是想知道我在应用启动时有 homeViewController,它是一个导航控制器。我应该把这段代码放在那个类中还是创建另一个?
  • 更新了答案以包含导航控制器。如答案所示,所有“内部”控制器均源自基本控制器。您可以将 SupportingLandscapeScreenTypes 定义为静态以避免多个实例
  • 我将导航控制器用于您可能不需要从中派生或更改它的其他目的
  • 你能解释一下你是如何从基础派生控制器的吗?
猜你喜欢
  • 2019-11-18
  • 1970-01-01
  • 2019-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2020-02-01
  • 2020-04-25
相关资源
最近更新 更多