iOS 8自定义键盘高度在纵向、横向旋转后恢复到纵向时的变化

4
我们可以在viewdidload中像苹果开发者网站上展示的代码一样创建自定义键盘。
self.nextKeyboardButton = [UIButton buttonWithType:UIButtonTypeSystem];

[self.nextKeyboardButton setTitle:NSLocalizedString(@"Next Keyboard", @"Title for 'Next Keyboard' button") forState:UIControlStateNormal];
[self.nextKeyboardButton sizeToFit];
self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = NO;

[self.nextKeyboardButton addTarget:self action:@selector(advanceToNextInputMode) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:self.nextKeyboardButton];

NSLayoutConstraint *nextKeyboardButtonLeftSideConstraint = [NSLayoutConstraint constraintWithItem:self.nextKeyboardButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0];
NSLayoutConstraint *nextKeyboardButtonBottomConstraint = [NSLayoutConstraint constraintWithItem:self.nextKeyboardButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
[self.view addConstraints:@[nextKeyboardButtonLeftSideConstraint, nextKeyboardButtonBottomConstraint]];

然而,有一个问题。当我们在纵向模式下运行该程序时,自定义键盘的高度约为220。然后我们切换到横向模式。它小于220。奇怪的是,当我回到纵向视图时,高度不再是220,而是与横向高度相同。它永远不会回到原始高度。因此,有没有一种方法可以更改要添加子视图的视图的高度?

1个回答

8

这可能是苹果键盘约束问题中的一个错误,在旋转时我在日志中看到以下消息:

 Unable to simultaneously satisfy constraints.
        Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
    (
        "<NSLayoutConstraint:0xfd04d80 V:[_UIKBCompatInputView:0xf97a580(216)]>",
        "<NSLayoutConstraint:0xe325bf0 V:[_UIKBCompatInputView:0xf97a580(162)]>"
    )

    Will attempt to recover by breaking constraint 
    <NSLayoutConstraint:0xfd04d80 V:[_UIKBCompatInputView:0xf97a580(216)]>

    Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
    The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

我已经为此提交了一个错误报告。


嘿,Woof - 你是如何使用键盘扩展进行日志记录的?我一直无法成功地将日志打印到控制台中。 - Spentak
更新到最新的Xcode应该可以修复在设备上运行时的日志问题。有时候仍然会出现问题,您可以尝试卸载该应用程序。 - woof
一直在使用最新的Xcode,但没有成功。也许只是键盘扩展(与其他扩展不同)。 - Spentak
苹果已经修复了他们的约束错误,如果您使用自动布局并且在约束方面遇到问题,这也会出现问题。 - woof
它并非每次都出现,只是偶尔会出现。当没有这样的错误出现时,在viewDidAppear时self.view中就没有任何限制。 - Henry
对我来说,唯一有效的解决方案是@skyline75489所提供的这个被采纳的答案:https://dev59.com/_WAf5IYBdhLWcg3w52A_#BjUPoYgBc1ULPQZFVNTT - lifjoy

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