2017-03-14 4 views
2

Je dois vérifier si mon appareil a changé d'orientation dans iOS 8+.iOS - Mauvaises limites UIScreen dans viewWillTransition pour iPad

Mon approche est la suivante:

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

il fonctionne très bien dans l'iPhone mais l'iPad isLand a déjà la nouvelle valeur qui devrait être après l'achèvement de l'orientation, de sorte que:

Portrait> Paysage: true -> true

Paysage> Portrait: false -> false

Selon la documentation de les limites devraient changer avec l'orientation de sorte qu'il devrait avoir des limites avant/après, n'est-ce pas?

UIScreen bornes principales:

Ce rectangle est spécifié dans l'espace de coordonnées actuel, qui tient compte des rotations de l'interface en vigueur pour le dispositif. Par conséquent, la valeur de cette propriété peut changer lorsque l'appareil pivote entre les orientations portrait et paysage.

considérant qu'il fonctionne très bien à la fois l'iPhone et l'iPad si j'utilise les limites du régulateur de courant de vue racine comme celui-ci:

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

Portrait> Paysage: false -> true

Paysage> Portrait: true -> false

+0

La méthode viewWillTransition() est-elle appelée avant ou après viewDidAppear? – luca

Répondre

2

Vous devriez plutôt utiliser containerView du contexte de coordinateur.

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

} 

Si vous souhaitez obtenir de plus amples informations au sujet de la transition que vous pouvez utiliser le func view(forKey: UITransitionContextViewKey) et func viewController(forKey: UITransitionContextViewControllerKey) avec la touche .from.