ios - 不要在水平紧凑的环境中折叠 UISplitViewController

标签 ios iphone ipad cocoa-touch uisplitviewcontroller

UISplitViewController 的默认行为是当它从水平规则转换到水平紧凑环境时折叠。 是否有可能以某种方式覆盖它?

由于某些设计解决方案,我希望 splitView 始终展开。 我没有在 UISplitViewController 的文档中找到解决方案:属性 collapsed 是只读的,委托(delegate)方法仅用于“如何折叠”,而不用于“一点都不崩溃”。我正在使用 iOS9 SDK。

最佳答案

我想通了。 文档指出:

The split view controller determines the arrangement of its child view controllers based on the available space. In a horizontally regular environment, the split view controller presents its view controllers side-by-side whenever possible. In a horizontally compact environment, the split view controller acts more like a navigation controller, displaying the primary view controller initially and pushing or popping the secondary view controller as needed.

因此 splitView 在 Compact 水平环境中总是会折叠,在 Regular 中会展开。解决方法就是在我们要展开的时候告诉splitView它有Regular horizo​​ntal environment。思路取自WWDC 2014 Video “使用 UIKit 构建自适应应用程序”。不幸的是,sample code他们在视频中提到的不包括这种情况。但我们的想法是为 SplitViewController 创建包含 ViewController,我们可以在其中使用方法 setOverrideTraitCollection:forChildViewController:

覆盖 SplitViewController 的特征集合
@implementation ContainingViewController

- (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nil bundle:nil];
    if (self)
    {
        _splitViewController = [[UISplitViewController alloc] initWithNibName:nil bundle:nil];
        _splitViewController.viewControllers = <view controllers>;

        [self addChildViewController:_splitViewController];
        [self.view addSubview:_splitViewController.view];

        UITraitCollection *horizontallyRegularTraitCollection = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassRegular];
        [self setOverrideTraitCollection:horizontallyRegularTraitCollection forChildViewController:_splitViewController];
    }
    return self;
}

通过这段代码,SplitViewController 将始终具有 Regular 水平特征集合,从而扩展。

关于ios - 不要在水平紧凑的环境中折叠 UISplitViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32656876/

相关文章:

ios - self.navigationController.navigationBar setHidden :NO not working when view controllers are swapped out

ios - 新设备的导航栏高度是多少?

iphone - 为什么时间在这里卡住?我每次都能获得自 1970 年以来新调用的秒数吗?

iphone - 可以在 iPad 上使用 NFC 吗?

iphone - 在 iPhone App 首次启动时显示信息 imageview

ios - 将视频添加到iPad应用程序中

ios - Xamarin.Forms - 强制重绘 ListView 布局

iphone - 如何在 iOS 中插入和覆盖音频文件

ios - 在 UITests 中解决 "Main Thread Checker: UI API called on a background thread: -[UIApplication setNetworkActivityIndicatorVisible:]"

iphone - 创建没有 Storyboard的多 View 应用程序?