12

I’m trying to update the viewWillTransition(to size: with coordinator:) method for iPhoneX. But I can’t get the destinations of the safeAreaInsets values. Please help!

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()
}
4

2 回答 2

22

我在 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)
}
于 2017-10-05T09:09:39.690 回答
0

我也有这个问题。经过试验,我找到了一个很好的解决方法,而无需等待过渡完成。

  1. 有一个视图(我们称之为SizeListenerView)锚定到安全区域,这样我们只需要监听这个视图的大小变化

SizeListenerView

    override var bounds: CGRect {
        didSet {
            // now you can access the bounds here
        }
    }
于 2019-02-08T00:42:54.387 回答