gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:13:51 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com