如何在viewWillTransitionToSize中获取safeAreaInsets(iPhone X)

66tom

我正在尝试更新viewWillTransition(to size: with coordinator:)iPhoneX方法。但是我无法获取safeAreaInsets值的目的地。请帮忙!

override func viewWillTransition(to size: CGSize,
        with coordinator: UIViewControllerTransitionCoordinator) {

    super.viewWillTransition(to: size, with: coordinator)

    if #available(iOS 11.0, *) {
        if let window = UIApplication.shared.keyWindow { 
            let insets = window.safeAreaInsets
            contentFrame = CGRect(x:insets.left, y:insets.top,
                width:size.width - insets.left - insets.right,
                height:size.height - insets.top - insets.bottom)
        }
    } else {
        contentFrame = CGRect(x:0,y:0,width:size.width, height:size.height)
    }
    self.updateViews()
}
66tom

我在Stackoverflow Japan获得了有效的解决方案。

它只是在UIViewControllerTransitionCoordinator.animate(alongsideTransition:completion:)闭包中插入插图,如下所示:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)

    coordinator.animate(alongsideTransition: { (context) in
        ...

        let insets = ...safeAreaInsets

        ...
    }, completion: nil)
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章