旋转iOS设备时如何处理键盘布局变化

3
我有两种略微不同的键盘布局,取决于设备的方向。 当我首次加载键盘时,它看起来很好。 这是纵向版本:

Portrait Keyboard

最初的约束设置发生在UIInputViewController子类的-viewDidLoad中。
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];
}

看起来在视图更改后,它突然占用了整个屏幕而不仅仅是键盘区域。 有什么想法可以修复这个问题吗?谢谢。


不确定,但可能是自动布局的问题。我曾经遇到库尔德语键盘设计的困难,所以我使用了自动布局,这可能会有所帮助。 - Shobhakar Tiwari
我认为将屏幕宽度与高度进行比较并不是最好的想法。相反,可以尝试访问状态栏方向,例如 [[UIApplication sharedApplication] statusBarOrientation]。或者您可以尝试获取设备的方向,例如 [[UIDevice currentDevice] orientation] - mbm29414
此外,我认为您的限制是在纵向模式下使“0x”按钮、空格按钮和返回按钮具有固定宽度,而地球键占据其余部分。然后,我猜测您应用了纵横比,这可以解释第一个“错误”的键盘看起来很合理。 - mbm29414
@SKT,您是建议我使用还是不使用自动布局?我不确定IB之外的默认设置是什么。 - NickEntin
让我们在聊天中继续这个讨论 - mbm29414
显示剩余3条评论
1个回答

3
问题似乎与隐式地覆盖包含键盘的视图的高度有关。虽然我没有看到任何会强制调整容器视图大小的东西,但是显式设置容器视图的高度可以得到良好、可重复的结果。
我从苹果公司的应用扩展编程指南:自定义键盘中得到了手动约束键盘高度的想法。
具体而言,相关信息如下:

您可以使用自动布局来调整自定义键盘主视图的高度。默认情况下,根据屏幕大小和设备方向,自定义键盘的大小与系统键盘匹配。自定义键盘的宽度始终由系统设置为等于当前屏幕宽度。要调整自定义键盘的高度,请更改其主视图的高度约束。

以下代码行显示了您可能定义和添加此类约束的方式:

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];

注意

iOS 8.0之后,您可以在自定义键盘的主视图首次绘制在屏幕上后随时调整其高度。

根据此信息,以下是模板答案中的重要方法:

- (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];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接