iOS:设备旋转后如何运行功能(Swift)

标签 ios swift rotation

我有一个没有使用自动布局的 UIView,一些组件是根据它们在主视图中的 X 和 Y 坐标百分比显示的。

以前我会运行一个函数来更新它们在 didRotateFromInterfaceOrientation 中的位置,但是我发现这在 iOS8 中现在已被弃用。

我查看了 viewWillTransitionToSize,但它给出了奇怪的结果,而且似乎没有 viewDidtransitionToSize 函数。

是否有一种简单的方法(在 Swift 中)在设备旋转后运行函数?

最佳答案

viewWillTransitionToSize使用 UIViewControllerTransitionCoordinator 调用委托(delegate)方法符合对象。协议(protocol)声明的方法是 animateAlongsideTransition(_:animation, completion:) .您可以使用它在转换完成后执行代码。

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    coordinator.animate(alongsideTransition: nil) { _ in
        // Your code here
    }
}

关于iOS:设备旋转后如何运行功能(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26943808/

相关文章:

iOS swift 3 : Comparing One all elements of Array in another array

ios - 在所有 viewDidLoad 函数中重复相同的语句可能的扩展文件

blackberry - 在支持加速度计的黑莓设备上拍照后透视图像错误

html - 使用 -webkit-transform 时固定位置不起作用

ios - 使用按钮将数据传递到另一个 UIViewController

ios - swift 2 - SWRevealViewController : Remove interaction on frontview when menu is open

ios - 样本: Tableview of custom cells to another tableview of custom cells

ios - 滚动时重复使用一行内的项目

ios - 带有异步 UIImageView 的自动布局 UITableViewCell 不工作

ipad - UITabBarController 中的 UIViewController 和 UISplitViewController shouldAutorotateToInterfaceOrientation