UIDeviceOrientationDidChange通知来处理,后来发现个别情况计算出来的大小位置不对,经过排查发现我的代码里只处理是水平还是垂直的逻辑,然而device的orientation却还包含了face up和face down的情况,并且即便你的app并不支持这俩个orientation,但是由于orientation确实改变了,所以还是会接到。但我确实不需要这俩个orientation,经过查找资料,得到如下答复“
Apple recommends against using device orientation for view layout. Instead, each view controller has an interfaceOrientation property, and UIApplication
has a statusBarOrientation property, both of which will return the current interface orientation, which is suitable for view layout.
To monitor for changes, there are UIViewController
methods like willRotateToInterfaceOrientation:duration: that will be called and notifications such as UIApplicationWillChangeStatusBarOrientationNotification that will be posted when an interface orientation change occurs.
至此,这个关于屏幕旋转的通知基本上就清楚了,如果你只关心水平和垂直之间的改变,那么请选择接收 UIApplicationWillChangeStatusBarOrientationNotification,因为接收UIDeviceOrientationDidChange所带来的face up和face down可能会给你的逻辑处理带来意想不到的BUG,如果你需要face up和face down的改变,比如有些需求需要face down的时候屏幕变暗等,那么你需要接收它来详细的区分各种orientation~
总之,慎重使用UIDeviceOrientationDidChange来确定view的layout(其实最好不要用,官方不推荐,本身也有更好的)~