我有两种略微不同的键盘布局,取决于设备的方向。 当我首次加载键盘时,它看起来很好。 这是纵向版本:
此部分代码能够正常加载,不论初始方向为何。然而,一旦我旋转设备,一切就出了问题。
接着回到竖屏状态:
参考正确的横屏界面如下图所示:
我在-updateViewConstraints方法中更新约束如下:
portraitConstraints = [self constraintsForOrientation:UIInterfaceOrientationPortrait];
landscapeConstraints = [self constraintsForOrientation:UIInterfaceOrientationLandscapeRight];
[self.view addConstraints:portraitConstraints];
[self.view addConstraints:landscapeConstraints];
if ([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height) {
[NSLayoutConstraint deactivateConstraints:landscapeConstraints];
} else {
[NSLayoutConstraint deactivateConstraints:portraitConstraints];
}
此部分代码能够正常加载,不论初始方向为何。然而,一旦我旋转设备,一切就出了问题。
接着回到竖屏状态:
参考正确的横屏界面如下图所示:
我在-updateViewConstraints方法中更新约束如下:
- (void)updateViewConstraints {
if ([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height) {
[NSLayoutConstraint deactivateConstraints:landscapeConstraints];
[NSLayoutConstraint activateConstraints:portraitConstraints];
} else {
[NSLayoutConstraint deactivateConstraints:portraitConstraints];
[NSLayoutConstraint activateConstraints:landscapeConstraints];
}
[super updateViewConstraints];
}
看起来在视图更改后,它突然占用了整个屏幕而不仅仅是键盘区域。 有什么想法可以修复这个问题吗?谢谢。
[[UIApplication sharedApplication] statusBarOrientation]
。或者您可以尝试获取设备的方向,例如[[UIDevice currentDevice] orientation]
。 - mbm29414