ios - 旋转时未调用 UIView 子类的方法 TraitCollectionDidChange(_ previousTraitCollection : UITraitCollection? )

标签 ios

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/

相关文章:

android - Phonegap - 对 Android 和 IOS 感到困惑

html - UIWebView 中嵌入的 YouTube 视频无法播放

ios - iPhone ios 上的 swift 3 中的弹出窗口

ios - Swift 3 更新错误 - 在展开可选值时意外发现 nil

iphone - 如何将SubView的Frame坐标系转换为Self View的坐标系

ios - SKScene iPad 高度宽度反转

ios - EZAudio 停止获取音频

ios - 如何删除 WKWebview cookies

android - 是否可以在 React.js Native 中使用 eval() 来定义应用程序中的新体验?

ios - CGAffine 使用 UIPanGestureRecognizer 转换 UIView