我正在尝试为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()
}
答案 0 :(得分:16)
我在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)
}
答案 1 :(得分:0)
我也有这个问题。经过试验后,我发现了一个不错的解决方法,而无需等待过渡完成。
UITextfields
),因此我们只需要侦听此视图的大小变化在func textFieldDidBeginEditing(UITextField)
SizeListenerView