我正在尝试更新iPhoneX的viewWillTransition(to size: with coordinator:)方法。
但是我无法获得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()
}

最佳答案

我在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)
}

关于ios - 如何在viewWillTransitionToSize中获取safeAreaInsets(iPhone X),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46538117/

10-09 02:21