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

Don't collapse UISplitViewController in horizontally compact environment

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

由于某些设计解决方案,我希望 splitView 始终扩展。 我没有在 UISplitViewController 的文档中找到解决方案: 属性 collapsed 是只读的,委托方法仅适用于 "how to collapse",但不适用于 "don't collapse at all"。我正在使用 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 "Building Adaptive Apps with UIKit". Unfortunately, the 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 水平特征集合,从而扩展。