强制 UISplitViewController 始终(仅)横向显示 master(在 iPhone 6 Plus 上)
Force UISplitViewController to always show master (only) in landscape (on iPhone 6 Plus)
在通用应用程序中,我无法找到始终(且仅)显示主控制器的方法,即使在 iPhone 6 Plus 横向旋转时也是如此。
我想实现的是只在 iPad 而不是 iPhone 上看到 UISplitViewController 在运行,不知道是否可行
委托方法没有帮助:
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
}
您需要覆盖 UISplitViewController 的特征集合以始终具有紧凑的大小 class。为此,您需要插入一个容器视图控制器作为 UISplitViewController 的父级:
- 将您的 UISplitViewController 嵌入到 ContainerViewController 中
将以下代码添加到您的容器视图控制器 subclass 以覆盖您的子视图控制器的特征集合:
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)
}
}}
中有很好的解释
特里的回答很好!此处已更新为 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 class 中改用以下代码:
override func overrideTraitCollection(forChild childViewController: UIViewController) -> UITraitCollection? {
if UIDevice.current.userInterfaceIdiom != .pad {
return UITraitCollection(horizontalSizeClass: .compact)
} else {
return super.traitCollection
}
}
在通用应用程序中,我无法找到始终(且仅)显示主控制器的方法,即使在 iPhone 6 Plus 横向旋转时也是如此。
我想实现的是只在 iPad 而不是 iPhone 上看到 UISplitViewController 在运行,不知道是否可行
委托方法没有帮助:
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
}
您需要覆盖 UISplitViewController 的特征集合以始终具有紧凑的大小 class。为此,您需要插入一个容器视图控制器作为 UISplitViewController 的父级:
- 将您的 UISplitViewController 嵌入到 ContainerViewController 中
将以下代码添加到您的容器视图控制器 subclass 以覆盖您的子视图控制器的特征集合:
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) } }}
特里的回答很好!此处已更新为 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 class 中改用以下代码:
override func overrideTraitCollection(forChild childViewController: UIViewController) -> UITraitCollection? {
if UIDevice.current.userInterfaceIdiom != .pad {
return UITraitCollection(horizontalSizeClass: .compact)
} else {
return super.traitCollection
}
}