2017-10-09 1 views
0

방향 변경 후 UIScreen.main.bounds.height을 쿼리 할 때 이상한 결과가 나타납니다. 어쩌면 이것이 올바른 방법이 아닐 수도 있습니다. 이 새로운 화면 높이의 75 %에 제약 조건을 설정하는 함수를 호출방향 변경 후 화면 높이를 얻는 방법은 무엇입니까?

NotificationCenter.default.addObserver(self, selector: #selector(self.orientationChange), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) 

:

은 내가 NSNotification.Name.UIDeviceOrientationDidChange 이벤트를 수신 관찰자가 있습니다. 이것은 iPhone에서 잘 작동하지만 iPad는 잘못된 화면 높이를 반환합니다.

아이 패드가 풍경 방향으로 UIScreen.main.bounds.height에있는 경우

세로 방향으로 그 반대의 높이와 같은 값을 반환합니다.

func orientationChange() { 
    // This will print the correct value on iPhone and iPad. 
    if UIDevice.current.orientation.isLandscape { 
     print("Landscape") 
    } else { 
     print("Portrait") 
    } 

    let screenSize = UIScreen.main.bounds 
    let screenHeight = screenSize.height 
    self.searchViewHeightConstraint.constant = screenHeight * 0.75 

    // Correct value on iPhone. Incorrect on iPad. 
    print("screenHeight: '\(screenHeight)'") 

    UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: { 
     self.searchView.superview?.layoutIfNeeded() 
    }, completion: nil) 
} 

또한 방향 변경을 모니터링하는 viewWilltransition 방법 건너했지만이 위의 방법과 정반대 방식으로 동작합니다. 즉. 높이가 아이폰 아이 패드에 대한 올바른하지만 잘못된 :

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { 
    let screenSize = UIScreen.main.bounds 
    let screenHeight = screenSize.height 
    self.searchViewHeightConstraint.constant = screenHeight * 0.75 

    // Correct value on iPad. Incorrect on iPhone. 
    print("screenHeight: '\(screenHeight)'") 
} 

무엇 아이폰과 아이 패드 사이의 일관성없는 행동에 대한 이유이며 NotificationCenter 방향 변경을 모니터링하는 올바른 접근 방식을 사용한다?

+0

'screenHeight' 값이 더 크거나 작습니까? – Srdjan

+0

screensize = UIScreen.main.bounds 사용을 중지하고 대신 크기를 사용하면 메서드에서 사용자에게 제공됩니다. 당신은 할 수 있습니다. size.height * 0.75 –

+0

또한 제약 조건을 사용하고 있고 view의 높이와 같은 searchView를 가지고 있다면 같은 heighViewConstraint에서 언제나 그 크기가 될 경우 수정자를 0.75로 설정할 수 있습니다 . 그럼 당신이 얻을 것이다 무엇 회전에보기 자체를 업데이 트하고 전혀 논리를 할 필요가 없을 것입니다. –

답변

1

사용해야하는 것은 viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)입니다. 이렇게하면 크기를 알 수 있고 알림을 사용하는 것보다 훨씬 신뢰할 수 있습니다. 당신이 UIViewControllerTransitionCoordinator의 내부, 애니메이션을 원한다면

또한, 당신은 방법이 아이 패드와 아이폰 모두 스위프트 2.3에 나를 위해 작동 animate(alongsideTransition animation: ((UIViewControllerTransitionCoordinatorContext) -> Swift.Void)?, completion: ((UIViewControllerTransitionCoordinatorContext) -> Swift.Void)? = nil) -> Bool

+0

나는 이미 이것을 시도하고 내 질문에 코드의 정반대로 행동. 즉. 그것은 iPad에서 올바른 치수를 표시하지만 iPhone에서 잘못된 치수를보고합니다. – Turnip

+0

애니메이션을 사용해 보았습니까? (전환과 함께 ...)? 나는 이것을 사용했고 그 방법의 완성에 모든 것이 제자리에 떨어지는 것처럼 보였다. –

+0

그래서 테스트 프로젝트를 만들고 아이폰의 크기를 출력 할 때 나는 가로 (736.0, 414.0)와 세로 (414.0, 736.0)를 얻었고 iPad의 경우는 가로가 1024.0,768.0' 및'초상화 (768.0, 1024.0)'. 이것들은 iPad Air 2와 iPhone 8이었습니다. –

0

을 활용할 수 있습니다.

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { 
    print(size.height) 

    if UIDevice.currentDevice().orientation.isLandscape { 
    print("Landscape") 
    print(size.height) 

    } 
else { 
     print("Portrait") 
     print(size.height) 
    } 

} 
관련 문제