私のデバイスがiOS 8+でデバイスの向きを変更したかどうかを確認する必要があります。iOS - iPad用のviewWillTransitionの間違ったUIScreen境界
私のアプローチは次のとおりです。
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主境界:
この矩形は が考慮デバイスに有効な任意のインターフェース回転を要する現在の座標空間で指定されています。 したがって、デバイス が縦向きと横向きの間で回転すると、このプロパティの値が変更されることがあります。
私はこのような現在のルートビューコントローラの境界を使用する場合には、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
is viewDidAppearの前後にviewWillTransition()メソッドが呼び出されますか? – luca