首页 > 解决方案 > UITraitsCollection 是在 IB/Storyboard 中区分 iPad Landscape 与 Portrait AutoLayout 的路径吗

问题描述

问: 我想在 iPad 上的纵向和横向之间有不同的布局。(我在 iPhone 上成功但 iPad 共享相似的尺寸等级)

我做了什么/尝试过:

通过 Google 和 SO 阅读和搜索,我还没有找到解决方案。我所学到的是关于覆盖UITraitsCollection,如果可能的话,我想强制 iPad 横向配置遵循我已经为 iPhone 横向模式设置的相同配置hCompact

我尝试了这个解决方案https://stackoverflow.com/a/60409218/14414215但不确定将它放在哪里以及何时放置在UIViewController运行时,应用程序甚至不会调用它。(这可能是因为我在 childViewController 中没有我的视图?)

此处的这篇文章还引用了UITraitsCollection https://stackoverflow.com/a/41374705/14414215但是缺少示例。

我相当确定这是许多应用程序正在做的事情,但我无法找到解决方案。

供参考:这也是我想在 iPad 上实现的。(iPhone 使用来自@DonMag 的这个解决方案效果很好)

StackView 布局并排并在彼此之上(如 zStack)

标签: iosswiftautolayoutuitraitcollection

解决方案


使用不同的设备屏幕纵横比iPad 上的多任务处理能力,我们鼓励您考虑尺寸而不是方向

如果您的应用程序正在使用 Slide Over / Split View 运行,则wC hR即使设备处于“横向”状态,尺寸等级也可以。

如果要根据纵横比更改布局:

  • 如果宽度 > 高度使用“横向”布局
  • 如果高度 > 宽度使用“纵向”布局

您可能想要创建自己的约束集合并在viewWillTransition(to size:...)

如果您想坚持使用 Trait 变体,您将在尝试为控制器的“根”视图操作它们时遇到问题。

可以尝试使您的控制器成为视图控制器,方法是将其嵌入UIContainerView或通过代码添加,然后实现(在“父”视图控制器中):

class ParentViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if let vc = storyboard?.instantiateViewController(withIdentifier: "myVC") as? MyRealViewController {
            addChild(vc)
            view.addSubview(vc.view)
            vc.didMove(toParent: self)
        }
        
    }
    
    override func overrideTraitCollection(forChild childViewController: UIViewController) -> UITraitCollection? {
        if childViewController.view.bounds.width > childViewController.view.bounds.height {
            let collections = [UITraitCollection(horizontalSizeClass: .regular),
                               UITraitCollection(verticalSizeClass: .compact)]
            return UITraitCollection(traitsFrom: collections)
        }
        return super.overrideTraitCollection(forChild: childViewController)
    }
    
}

推荐阅读