gpt4 book ai didi

ios - 设备旋转时切换 View Controller

转载 作者:搜寻专家 更新时间:2023-11-01 07:23:42 25 4
gpt4 key购买 nike

我有两个 View Controller ;一个用于风景,另一个用于肖像。如何在设备旋转时以编程方式切换这些,就像 native iPhone 计算器一样。

此外,纵向和横向 View Controller 在 xcode 和 swift 中彼此不同。

如有任何帮助,我们将不胜感激。

最佳答案

处理这种事情的现代方法是使用一个 Controller 来适应不同的尺寸,不仅可以处理各种可能的屏幕尺寸,还可以处理 iPad 的 Slide Over 和 Split View 多任务处理。您应该像计算器示例一样对 UI 进行重大更改的地方是按大小类别区分,在此处解释:

Size-Class-Specific Layout

Storyboards in Interface Builder by default use size classes. Size classes are traits assigned to user interface elements, like scenes or views. They provide a rough indication of the element’s size. Interface Builder lets you customize many of your layout’s features based on the current size class. The layout then automatically adapts as the size class changes. Specifically, you can set the following features on a per-size-class basis:

  • Install or uninstall a view or control.
  • Install or uninstall a constraint.
  • Set the value of select attributes (for example, fonts and layout margin settings).

When the system loads a scene, it instantiates all the views, controls, and constraints, and assigns these items to the appropriate outlet in the view controller (if any). You can access any of these items through their outlets, regardless of the scene’s current size class. However, the system adds these items to the view hierarchy only if they are installed for the current size class.

As the view’s size class changes (for example, when you rotate an iPhone or switch an iPad app between full-screen and Split View), the system automatically adds items to or removes them from the view hierarchy. The system also animates any changes to the view’s layout.

优秀教程可以在raywenderlich.com找到:

关于ios - 设备旋转时切换 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37346816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com