使用静态UITableView时限制视图控制器的方向

本文关键字:视图 控制器 方向 静态 UITableView | 更新日期: 2023-09-27 18:24:14

我已经用iOS Designer(Objective-C世界中的IB)设置了一个静态UITableView。但方向改变了,尽管我想限制它。

我做了以下工作:

模拟度量下的属性中,我选择纵向作为方向。然后我为我的UITableViewController实现以下功能:

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

调用GetSupportedInterfaceOrientations,返回Portrait,但视图仍在旋转。我缺少什么?

编辑:

我使用了"视图定向"中讨论的方法。这适用于我的视图控制器。静态UITableViewController在堆栈上以这种方式推送:

this.PresentViewController (new UINavigationController(myStaticTableViewController), true, null);

这里使用UINavigationController的标准实现。我还用实现的CustomNavigationController进行了尝试

partial class CustomNavigationController : UINavigationController
{
    public CustomNavigationController (IntPtr handle) : base (handle)
    {
    }
    public override bool ShouldAutorotate ()
    {
        return TopViewController.ShouldAutorotate();
    }
    public override UIInterfaceOrientation PreferredInterfaceOrientationForPresentation ()
    {
        return TopViewController.PreferredInterfaceOrientationForPresentation ();
    }
}

但我不能做这种

this.PresentViewController (new CustomNavigationController(myStaticTableViewController), true, null);

因为它无法将我的表视图控制器转换为CCD_ 7。也许这就是它不尊重界面方向的原因。我有什么解决方案?

使用静态UITableView时限制视图控制器的方向

似乎我只需要按照链接线程中的说明添加另一个构造函数。现在我的CustoMNavigationController看起来是这样的:

partial class CustomNavigationController : UINavigationController
{
    public CustomNavigationController(UIViewController rootViewController) : base(rootViewController)
    {
    }
    public CustomNavigationController (IntPtr handle) : base (handle)
    {
    }
    public override bool ShouldAutorotate ()
    {
        return TopViewController.ShouldAutorotate();
    }
    public override UIInterfaceOrientation PreferredInterfaceOrientationForPresentation ()
    {
        return TopViewController.PreferredInterfaceOrientationForPresentation ();
    }
}

现在我可以使用了

this.PresentViewController (new CustomNavigationController(myStaticTableViewController), true, null)

一切都按预期进行。