方向更改后如何获取屏幕高度?

How to get the screen height after orientation change?

在方向更改后查询 UIScreen.main.bounds.height 时,我得到了一些奇怪的结果。也许这不是正确的做法。

我有一个监听 NSNotification.Name.UIDeviceOrientationDidChange 事件的观察者:

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

这会调用一个函数,将约束设置为新屏幕高度的 75%。这在 iPhone 上工作正常,但 iPad return 屏幕高度错误。

如果 iPad 处于 横向 方向 UIScreen.main.bounds.height 将 return 值等于 纵向的高度 方向,反之亦然。

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 监视方向变化的方法,但这与上述方法的行为方式完全相反。 IE。高度在 iPad 上是正确的,但在 iPhone 上不正确:

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

iPhone 和 iPad 之间这种不一致行为的原因是什么?使用 NotificationCenter 监控方向变化的正确方法是什么?

你应该使用的是 viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)。这将为您提供大小,并且比使用通知更可靠。

另外,如果你想做动画,你可以在 UIViewControllerTransitionCoordinator 中利用方法 animate(alongsideTransition animation: ((UIViewControllerTransitionCoordinatorContext) -> Swift.Void)?, completion: ((UIViewControllerTransitionCoordinatorContext) -> Swift.Void)? = nil) -> Bool

这在 Swift 2.3 中适用于 iPad 和 iPhone。

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

}

您需要使用:

dispatch_async(dispatch_get_main_queue()) { 
println("h:\(view.bounds.size.height)")
println("w:\(view.frame.size.width)")
         }

// 只需在dispatch_async

下编写代码

此代码将在轮换完成(在主队列中)并且新尺寸可用后执行。