UITraitEnvironment 的文档说:
This method is called automatically when the current trait collection changes. Overriding this method provides you with a way to customize behavior when the trait collection associated with the view changes. If you do not override this method, no special behavior is performed.
但是当我旋转模拟器时,我的 UIView 子类中的此方法不会被调用。
最佳答案
单独的 UIView 永远无法响应旋转变化。您需要从包含的 View Controller 中重新配置 View 的当前方向/大小。
如果您使用的是 iPad,即使是 iOS 8,特征集合也不会因旋转而改变;您仍将处于 H:Regular、V:Regular 状态。 您需要覆盖:
func viewWillTransitionToSize(_ size: CGSize, withTransitionCoordinator 协调器: UIViewControllerTransitionCoordinator)
响应 View Controller 因旋转而发生的尺寸变化。 (一定要调用 super!)
您可以通过比较尺寸的宽度和高度来推测 View Controller 的“方向”(尽管现代 iOS 编程会鼓励您停止考虑方向):
let isPortrait = size.height > size.width
关于ios - 旋转时未调用 UIView 子类的方法 TraitCollectionDidChange(_ previousTraitCollection : UITraitCollection? ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27174666/