Ios 如何在ViewSize(iPhone X)中获取安全区域插图

Ios 如何在ViewSize(iPhone X)中获取安全区域插图,ios,swift,iphone-x,Ios,Swift,Iphone X,我正在尝试更新iPhoneX的viewWillTransition(到size:with-coordinator:)方法。 但我无法获取安全区域插入值的目的地。 请帮忙 override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { super.viewWillTransition(to: size, with: co

我正在尝试更新iPhoneX的
viewWillTransition(到size:with-coordinator:)
方法。 但我无法获取安全区域插入值的目的地。 请帮忙

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

我在日本得到了有效的解决方案

只需在
UIViewControllerTransitionCoordinator.animate中获得插图(沿过渡:完成:)
闭包,如下所示:

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

我也有这个问题。经过实验,我发现了一个很好的解决方法,不用等待转换完成

  • 有一个锚定到安全区域的视图(我们称之为
    SizeListenerView
    ),这样我们只需要监听此视图的大小更改
  • SizeListenerView

        override var bounds: CGRect {
            didSet {
                // now you can access the bounds here
            }
        }
    

    “我无法获取sefeAreaInsets值的目的地”是什么意思?有什么问题吗?你为什么要看窗户?你的代码毫无意义。你到底想做什么?我想要旋转后的值。在这一点上,它是在旋转之前
    viewwilltransitionOnSize
    只是关于一件事:大小。旋转后的大小值为
    size
    。你认为这与安全区域插图有什么关系?我再次问你:你为什么需要这些信息?你到底想做什么?(我要求第二次)做旋转动画例程。那么也许你应该说它们是什么。事实上,你需要这些信息,在代码中,是一种“臭味”。你以前没有注意到顶部和底部的布局指南,那么你为什么现在关心安全区域的插图呢?我再问你一次:有什么问题?你看,这是一个x-y问题。你没有问自己真正想知道什么,而是做了一个错误的假设,现在你为它不起作用而烦恼。这是行不通的;这是假的。你需要退后一步,问问你真正的问题。