ios - 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 的这个解决方案效果很好)
解决方案
使用不同的设备屏幕纵横比和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)
}
}
推荐阅读
- android - 为什么从Android中的本机程序连接到LocalServerSocket时连接被拒绝
- vb.net - 在哪里声明一个引用 VB 对象的公共对象?
- php - 如何 curl 转换为 php?curl 有效,但浏览器无效
- r - R:在 R 中绘制 3D 对象(虹膜数据)
- python - 硒“ERR_TUNNEL_CONNECTION_FAILED”
- math - 将角度映射到 0-1 范围
- html - 无法在容器中间设置输入和提交表单
- django - Django - ManyToMany - 如何过滤父对象的 ManyToMany
- node.js - Nodejs 相当于 c sscanf
- sed - 使用 REGEX 和 SED 以科学形式搜索和删除浮点数