ios - 如何根据 iOS 9 中尺寸类的变化交换 View Controller ?

标签 ios uiviewcontroller autolayout size-classes adaptive-layout

在我的应用程序中,当在小型设备(例如 iPhone 纵向)和大型设备(例如 iPad 全屏)上使用时,大多数 View 在 UI 设计上存在重大差异。基本架构保持不变,但每个 View Controller 都需要显示一个非常不同的 UI 元素集。

我可以通过根据尺寸等级安装和卸载元素以及自动布局约束来响应 Interface Builder 中的尺寸变化,但这将非常笨拙和脆弱。

在过去(~2014 年),Apple 的建议是:

如果您想根据设备是纵向还是横向显示相同的数据,方法是使用两个单独的 View Controller 。一个 View Controller 应该管理数据在主要方向(通常是纵向)上的显示,而另一个 View Controller 管理数据在备用方向上的显示。

尽管出现了 Size Classes,但我觉得这仍然是我特定设计的最佳解决方案(记住要考虑 size classes 和 split-view、slide-in。)

问题 1:我应该在我的应用程序中的什么时候检测特征变化?

问题 2:(假设我已经从 Storyboard 中加载了备用 View Controller )以响应特征更改,我应该如何最好地转换/呈现备用 View Controller ?

我在这里问同样的问题:Adaptive swapping of View Controllers in response to Size Class Trait changes iOS 9

因为没有人回答最初的问题。

最佳答案

如果您在谈论特征更改 运行时 ,例如设备旋转和 iPad 多任务,可以使用 viewWillTransitionToSize (参见 this question 示例代码)并在此方法中调整 View 层次结构(如果您愿意,可以使用动画)。只需使用普通 View 和 View Controller 呈现方法来添加/删除呈现的 Controller 或子 Controller 及其 View 。

如果您在谈论特征更改 在发布时 (iPad 与 iPhone),只需为这两种设备创建不同的 Storyboard或 Root View Controller 并将其设置在您的应用程序委托(delegate)中。但是,这种方法的不利之处在于,如果您没有额外的工作,就无法为横向的大尺寸 iPhone 提供类似 iPad 的体验。

不过,我想挑战你所说的:

I could respond to size changes in Interface Builder, by Installing and Uninstalling elements and Auto Layout Constraints according to size class, but this would be extremely unwieldy and fragile.



Xcode 6 及更高版本支持文档描述的“统一 Storyboard”,如下所示:

A storyboard can add or remove views and layout constraints based on the size class that the view controller is displayed in. Rather than maintaining two separate (but similar) storyboards, you can make a single storyboard for multiple size classes. First, design your storyboard with a common interface and then customize it for different size classes, adapting the interface to the strengths of each form factor.



Xcode 8 使设计这些高度自适应的 View 变得特别容易。您可以切换到特定尺寸特征的编辑模式,甚至可以预览 View Controller 在各种设备上的外观。在继续您当前的道路之前,我鼓励您深入研究 Xcode 8 中的改进(如果您还没有的话),看看您是否可以使统一的 Storyboard适用于您的用例。

关于ios - 如何根据 iOS 9 中尺寸类的变化交换 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39417058/

相关文章:

iphone - 使用单步旋转时如何实现流畅的动画/如何在旋转开始时获取新的帧大小?

iOS/Swift - 第一次使用后无法让键盘外观移动 View

iOS - 如何获取时区的原始偏移量?

ios - 使用 BOOL/完成 block 停止自动反转/无限重复 UIView 动画

ios - 如何使用自动布局来缩放所有屏幕尺寸的 View ?

更改设备方向时违反 iOS 自动布局约束

ios - Swift UITableView 自动高度

iphone - 来自应用程序委托(delegate)的 presentModalViewController

ios - 更改 View Controller

iOS - 嵌套的推送动画可能导致导航栏损坏