2017-03-14 27 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)") 
    } 
} 

은 아이폰이 아니라 아이 패드 isLand에서 잘 작동 그래서 방향 완료 후해야 이미 새 값이 있습니다

세로> 풍경 : true -> true

가로> 세로 : false -> false

설명서에 따르면 경계는 방향과 함께 변경되어야하므로 경계 이전/이후를 가져야합니다. 그렇지 않을까요?

UIScreen 주 경계이 사각형이 현재의 지정

장치에 대한 효과를 고려 어떤 인터페이스 회전 소요 공간 좌표. 따라서 장치가 세로 방향과 가로 방향으로 회전 할 때이 속성의 값이 변경 될 수 있습니다.

이 같은 현재의 루트 뷰 컨트롤러의 경계를 사용하는 경우가 아이폰과 아이 패드 모두 잘 작동하는 반면 :

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

} 

당신이 func view(forKey: UITransitionContextViewKey)func viewController(forKey: UITransitionContextViewControllerKey).from 키를 사용하여 사용할 수있는 전환에 대한 자세한 정보를 얻고 싶다면.