gpt4 book ai didi

iOS - iPad 的 viewWillTransition 中错误的 UIScreen 边界

转载 作者:可可西里 更新时间:2023-11-01 00:57:25 26 4
gpt4 key购买 nike

我必须检查我的设备在 iOS 8+ 中是否改变了方向。

我的做法是:

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

let isLand = UIScreen.main.bounds.width > UIScreen.main.bounds.height

coordinator.animate(alongsideTransition: nil) { _ in
let isLand2 = UIScreen.main.bounds.width > UIScreen.main.bounds.height


print("\(isLand) -> \(isLand2)")
}
}

它在 iPhone 中运行良好,但在 iPad 中,isLand 已经有了应该在定向完成之后的新值,所以:

人像 > 风景:true -> true

横向 > 纵向:false -> false

根据文档,边界应该随着方向改变,所以它应该有一个前/后边界,不是吗?

UIScreen main bounds:

This rectangle is specified in the current coordinate space, which takes into account any interface rotations in effect for the device. Therefore, the value of this property may change when the device rotates between portrait and landscape orientations.

如果我像这样使用当前 Root View Controller 的边界,它在 iPhone 和 iPad 上都能正常工作:

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

let isLand = UIApplication.shared.keyWindow!.rootViewController!.view.bounds.width > UIApplication.shared.keyWindow!.rootViewController!.view.bounds.height

coordinator.animate(alongsideTransition: nil) { _ in
let isLand2 = UIApplication.shared.keyWindow!.rootViewController!.view.bounds.width > UIApplication.shared.keyWindow!.rootViewController!.view.bounds.height


print("\(isLand) -> \(isLand2)")
}
}

纵向 > 横向:false -> true

横向 > 纵向:true -> false

最佳答案

您应该尝试使用协调器上下文的 containerView。

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

let isLand = coordinator.containerView.bounds.width > coordinator.containerView.bounds.height

coordinator.animate(alongsideTransition: nil) { _ in
let isLand2 = coordinator.containerView.bounds.width > coordinator.containerView.bounds.height

print("\(isLand) -> \(isLand2)")
}

}

如果您想获得有关转换的更多信息,您可以使用 func view(forKey: UITransitionContextViewKey)func viewController(forKey: UITransitionContextViewControllerKey) 使用 .from 键。

关于iOS - iPad 的 viewWillTransition 中错误的 UIScreen 边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42782594/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com