我的问题是,在 iOS 8 中,表单表格采用了“紧凑-宽度常规-高度”(即所有iPhone的限制条件)而不是“任何-任何”或“常规-宽度常规-高度”的限制条件。我在 iPhone 和 iPad 上有两种不同的设计,由于表单表格使用了 iPhone 的限制条件,我无法实现相同的设计。如果能帮忙解决这个问题,将不胜感激。
UIModalPresentationFullScreen
相同。setOverrideTraitCollection(_:forChildViewController:)
并覆盖子控制器的大小类约束。vc.presentationController.overrideTraitCollection
。翻译成中文即为"覆盖控制器特征集合"。 // in landscape mode, we want the horizontal traits to be the same as the main screen
if ( UIScreen.mainScreen.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassRegular){
//if you use UIModalPresentationFormSheet, the traits horizontal will be compact , even on iPad, so we have tell the presentationcontroller to force the horizontaltraits to regular
vc.modalPresentationStyle=UIModalPresentationFormSheet;
vc.presentationController.overrideTraitCollection = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassRegular ];
[_rootViewController presentViewController:vc animated:true completion:^{}];
}
我找到了一种不同的方法来解决这个问题,针对呈现的视图控制器,例如不是子视图控制器的表单表。
我重写了viewWillLayoutSubviews
,然后根据呈现的视图控制器的特征集进行布局。
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if let presenting = presentingViewController {
updateLayout(forSizeClass: presenting.traitCollection.horizontalSizeClass)
}
}
updateLayout(forSizeClass:)
是我们的函数,它可以执行我们需要支持特性环境的任何操作。
这种方法的好处是我们不需要显式地检查设备类型(.pad
或 .phone
),也不需要显式地设置视图大小的阈值(这可能会在未来发生变化)。这种方法本地支持 iPad 分屏视图,在呈现视图控制器达到 .compact
大小类时,允许您的布局回退到 iPhone 样式布局。
这里的主要假设是您的呈现视图控制器是全屏的。但对于表单表格来说,这通常是成立的。