gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:55:59 24 4
gpt4 key购买 nike

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 coordinator: 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/

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