处理旋转时更改 iOS 键盘布局
Dealing with changing iOS keyboard layout upon rotation
根据设备的方向,我的键盘有两种略有不同的布局。当我第一次加载键盘时,它看起来不错。这是纵向版本:
初始约束设置发生在 -viewDidLoad(属于 UIInputViewController 子类)
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];
}
似乎在视图更改后它突然占据了超过全屏而不仅仅是键盘区域。有什么办法可以解决这个问题吗?谢谢。
这个问题似乎与以某种方式隐式覆盖包含键盘的视图的高度有关。虽然我没有看到任何会强制调整容器视图大小的内容,但显式设置容器视图的高度又恢复了良好的、可重现的结果。
我从 Apple's App Extension Programming Guide: Custom Keyboard 得到了手动限制键盘高度的想法。
具体相关信息是这样的:
You can adjust the height of your custom keyboard’s primary view using Auto Layout. By default, a custom keyboard is sized to match the system keyboard, according to screen size and device orientation. A custom keyboard’s width is always set by the system to equal the current screen width. To adjust a custom keyboard’s height, change its primary view's height constraint.
The following code lines show how you might define and add such a constraint:
CGFloat _expandedHeight = 500;
NSLayoutConstraint *_heightConstraint =
[NSLayoutConstraint constraintWithItem: self.view
attribute: NSLayoutAttributeHeight
relatedBy: NSLayoutRelationEqual
toItem: nil
attribute: NSLayoutAttributeNotAnAttribute
multiplier: 0.0
constant: _expandedHeight];
[self.view addConstraint: _heightConstraint];
NOTE
In iOS 8.0, you can adjust a custom keyboard’s height any time after its primary view initially draws on screen.
以下是基于此信息的模板答案中的重要方法:
- (void)updateViewConstraints {
if (self.keyboardHeightConstraint == nil) {
// Just starting with SOME value for the height
self.keyboardHeightConstraint =
[NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0f
constant:500.0f];
[self.view addConstraint:self.keyboardHeightConstraint];
}
// Obviously, these values will be changed based on device AND orientation
// These are bogus values...
if ([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height) {
self.keyboardHeightConstraint.constant = 300.0f;
[NSLayoutConstraint deactivateConstraints:self.landscapeConstraints];
[NSLayoutConstraint activateConstraints:self.portraitConstraints];
} else {
self.keyboardHeightConstraint.constant = 400.0f;
[NSLayoutConstraint deactivateConstraints:self.portraitConstraints];
[NSLayoutConstraint activateConstraints:self.landscapeConstraints];
}
[super updateViewConstraints];
}
根据设备的方向,我的键盘有两种略有不同的布局。当我第一次加载键盘时,它看起来不错。这是纵向版本:
初始约束设置发生在 -viewDidLoad(属于 UIInputViewController 子类)
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];
}
似乎在视图更改后它突然占据了超过全屏而不仅仅是键盘区域。有什么办法可以解决这个问题吗?谢谢。
这个问题似乎与以某种方式隐式覆盖包含键盘的视图的高度有关。虽然我没有看到任何会强制调整容器视图大小的内容,但显式设置容器视图的高度又恢复了良好的、可重现的结果。
我从 Apple's App Extension Programming Guide: Custom Keyboard 得到了手动限制键盘高度的想法。
具体相关信息是这样的:
You can adjust the height of your custom keyboard’s primary view using Auto Layout. By default, a custom keyboard is sized to match the system keyboard, according to screen size and device orientation. A custom keyboard’s width is always set by the system to equal the current screen width. To adjust a custom keyboard’s height, change its primary view's height constraint.
The following code lines show how you might define and add such a constraint:
CGFloat _expandedHeight = 500;
NSLayoutConstraint *_heightConstraint =
[NSLayoutConstraint constraintWithItem: self.view
attribute: NSLayoutAttributeHeight
relatedBy: NSLayoutRelationEqual
toItem: nil
attribute: NSLayoutAttributeNotAnAttribute
multiplier: 0.0
constant: _expandedHeight];
[self.view addConstraint: _heightConstraint];
NOTE
In iOS 8.0, you can adjust a custom keyboard’s height any time after its primary view initially draws on screen.
以下是基于此信息的模板答案中的重要方法:
- (void)updateViewConstraints {
if (self.keyboardHeightConstraint == nil) {
// Just starting with SOME value for the height
self.keyboardHeightConstraint =
[NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0f
constant:500.0f];
[self.view addConstraint:self.keyboardHeightConstraint];
}
// Obviously, these values will be changed based on device AND orientation
// These are bogus values...
if ([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height) {
self.keyboardHeightConstraint.constant = 300.0f;
[NSLayoutConstraint deactivateConstraints:self.landscapeConstraints];
[NSLayoutConstraint activateConstraints:self.portraitConstraints];
} else {
self.keyboardHeightConstraint.constant = 400.0f;
[NSLayoutConstraint deactivateConstraints:self.portraitConstraints];
[NSLayoutConstraint activateConstraints:self.landscapeConstraints];
}
[super updateViewConstraints];
}