在通用应用程序中,我无法找到始终显示(且仅限)主控制器的方法,即使在横向旋转的iPhone 6 Plus上也是如此。
我想要实现的是看到UISplitViewController仅在iPad上运行而不在iPhone上运行,不知道是否可能
委托方法没有帮助:
func splitViewController(svc: UISplitViewController, shouldHideViewController vc: UIViewController, inOrientation orientation: UIInterfaceOrientation) -> Bool {
return false
}
func splitViewController(splitViewController: UISplitViewController, collapseSecondaryViewController secondaryViewController: UIViewController, ontoPrimaryViewController primaryViewController: UIViewController) -> Bool {
return true
}
答案 0 :(得分:5)
您需要覆盖UISplitViewController的特征集合,以便始终拥有紧凑的大小类。为此,您需要插入一个容器视图控制器作为UISplitViewController的父级:
将以下代码添加到容器视图控制器子类中以覆盖子视图控制器的特征集合:
class ContainerVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
performOverrideTraitCollection()
}
private func performOverrideTraitCollection() {
for childVC in self.childViewControllers {
setOverrideTraitCollection(UITraitCollection(horizontalSizeClass: .Compact), forChildViewController: childVC)
}
}}
答案 1 :(得分:0)
将UISplitController
子类化,并将preferredDisplayMode
设置为.AllVisible
,它只影响可以同时显示两个视图的模式。
快速
class SplitViewController: UISplitViewController {
override func viewDidLoad() {
super.viewDidLoad()
preferredDisplayMode = .AllVisible
}
}
Objective-C
- (void)viewDidLoad {
[super viewDidLoad];
self.preferredDisplayMode = UISplitViewControllerDisplayModeAllVisible;
}
这将使iPad Portrait&iPhone Plus Family看起来像这样:
答案 2 :(得分:0)
Terry的答案很棒!在这里,它已更新为Swift 5,并且只能在iPhone设备上运行,而不能在iPad上运行:
class ContainerVC: UIViewController {
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if UI_USER_INTERFACE_IDIOM() != .pad {
performOverrideTraitCollection()
}
}
private func performOverrideTraitCollection() {
for childVC in self.children {
setOverrideTraitCollection(UITraitCollection(horizontalSizeClass: .compact), forChild: childVC)
}
}
}
针对iOS 13的更新
上面的代码在iOS 13上不再可用。在ContainerVC类中使用以下代码代替:
override func overrideTraitCollection(forChild childViewController: UIViewController) -> UITraitCollection? {
if UI_USER_INTERFACE_IDIOM() != .pad {
return UITraitCollection(horizontalSizeClass: .compact)
} else {
return super.traitCollection
}
}