方向改变后,iPhone 和 iPad 之间的屏幕尺寸是否会不一致?

Should I expect inconsistencies in screen dimensions between iPhones and iPads after orientation changes?

在 iOS 9.3 iPhones 和 iPads(实际设备和模拟器中),我得到关于 [UIScreen mainScreen].bounds 在方向更改通知之后。

我的视图控制器在视图加载时添加方向更改通知:

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(deviceOrientationDidChangeNotification:)
 name:UIDeviceOrientationDidChangeNotification
 object:nil];

方向改变时的行为如下:

- (void) deviceOrientationDidChangeNotification:(NSNotification *) notification {
    CGRect frame = [UIScreen mainScreen].bounds;
    NSLog(@"Main screen is w=%f h=%f", frame.size.width, frame.size.height);
}

运行 这在 iPhone 上,方向更改后,日志消息指示与 post[=27 相对应的框架宽度和高度=] 方向改变屏幕尺寸。

运行 这在 iPad 上,方向更改后,日志消息指示框架宽度和高度与 pre 方向更改相对应屏幕尺寸。

这种不一致是否需要编码?我可以开始尝试检测方向是什么(横向或纵向),然后使用有意义的宽度和高度值,但这很老套。

正确的处理方式是避免使用通知系统,而是使用 - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator

UIViewController 上的方法,您希望在其中调整 UI 以进行方向更改。