방향 변경 후 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
방향 변경을 모니터링하는 올바른 접근 방식을 사용한다?
'screenHeight' 값이 더 크거나 작습니까? – Srdjan
screensize = UIScreen.main.bounds 사용을 중지하고 대신 크기를 사용하면 메서드에서 사용자에게 제공됩니다. 당신은 할 수 있습니다. size.height * 0.75 –
또한 제약 조건을 사용하고 있고 view의 높이와 같은 searchView를 가지고 있다면 같은 heighViewConstraint에서 언제나 그 크기가 될 경우 수정자를 0.75로 설정할 수 있습니다 . 그럼 당신이 얻을 것이다 무엇 회전에보기 자체를 업데이 트하고 전혀 논리를 할 필요가 없을 것입니다. –