2017-03-14 5 views
2

私のデバイスが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

+0

is viewDidAppearの前後にviewWillTransition()メソッドが呼び出されますか? – luca

答えて

2

コーディネーターコンテキストの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)") 
    } 

} 

あなたが.fromキーを使用してfunc view(forKey: UITransitionContextViewKey)func viewController(forKey: UITransitionContextViewControllerKey)を使用することができます移行に関する詳細な情報を取得したい場合。

関連する問題