【发布时间】:2014-11-10 21:24:31
【问题描述】:
我注意到,当我在打电话时,绿色的通话栏出现在屏幕顶部,我的自定义键盘不会出现(就好像它被跳过了一样)。于是调试了一下,发现是约束错误。
这是调试器中的错误:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The layout constraints still need update after sending -updateViewConstraints to <KeyboardViewController: 0x16dfbe30>.
KeyboardViewController or one of its superclasses may have overridden -updateViewConstraints without calling super or sending -updateConstraints to the view. Or, something may have dirtied layout constraints in the middle of updating them. Both are programming errors.'
*** First throw call stack:
(0x2ab43c1f 0x382eec8b 0x2ab43b65 0x2e6300ef 0x2b7cb5b9 0x2e630203 0x2e0ef4e9 0x2b7cb5b9 0x2e0ef2c3 0x2e630487 0x2e29ba8b 0x2e0004d7 0x2da28a0d 0x2da243e5 0x2da2426d 0x2da23c51 0x2da23a55 0x2dff8965 0x2ab0a3b5 0x2ab07a73 0x2ab07e7b 0x2aa56211 0x2aa56023 0x31e4f0a9 0x2e0621d1 0x389cf9fb 0x389d1021 0x2b916635 0x33956065 0x33955d3b 0x33956099 0x37c7a4fd 0x3886eaaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
请务必注意,在没有呼叫栏的情况下,无论是方向还是方向更改,我都不会收到任何错误/警告。我正在使用以下方法将键盘的高度从 216 调整到 270:
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
//Prepare our keyboard's values
self.portraitHeight = 270;
self.landscapeHeight = 190;
}
return self;
}
- (void)updateViewConstraints {
//Super
[super updateViewConstraints];
//Check if self.view exists
if (WIDTH(self.view) == 0 || HEIGHT(self.view) == 0) {return;}
//Remove any previous constraints
[self.inputView removeConstraint:self.heightConstraint];
//Define landscape mode
self.isLandscape = !(self.view.frame.size.width == ([[UIScreen mainScreen] bounds].size.width * ([[UIScreen mainScreen] bounds].size.width < [[UIScreen mainScreen] bounds].size.height)) + ([[UIScreen mainScreen] bounds].size.height *([[UIScreen mainScreen] bounds].size.width > [[UIScreen mainScreen] bounds].size.height)));
//Update view height depending on orientation
if (self.isLandscape) {
//Readjust our keyboard's height
self.heightConstraint.constant = self.landscapeHeight;
[self.inputView addConstraint:self.heightConstraint];
self.view.frame = CGRectMake(0, 0, WIDTH(self.view), HEIGHT(self.view));
} else {
//Re-adjust our keyboard's height
self.heightConstraint.constant = self.portraitHeight;
self.heightConstraint.priority = 990;
[self.inputView addConstraint:self.heightConstraint];
self.view.frame = CGRectMake(0, 0, WIDTH(self.view), HEIGHT(self.view));
}
}
&这是我的观点WillAppear:
- (void)viewWillAppear:(BOOL)animated {
//Update our keyboard's height when view appears
self.heightConstraint = [NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:self.portraitHeight];
self.heightConstraint.priority = 990;
[self.view addConstraint:self.heightConstraint];
}
** 注意:WIDTH() 和 HEIGHT() 是宏。
我在我的 xib 中使用自动布局。我不知道从哪里开始解决这个问题。
更新
感谢您帮助调试此@Remus!我现在正在尝试检测设备何时在我的 iOS 8 扩展程序中旋转,但我的 updateConstraints 方法仍未被调用。
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self.view setNeedsLayout];
[self.view updateConstraintsIfNeeded];
//I've also tried [self.view updateConstraints]; instead
}
- (void)updateConstraints {
NSLog(@"Check Width");
//Check if self.view exists
if (WIDTH(self.view) == 0 || HEIGHT(self.view) == 0) {return;}
NSLog(@"Remove Old Constraints");
//Remove any previous constraints
[self.inputView removeConstraint:self.heightConstraint];
NSLog(@"Define Landscape");
//Define landscape mode
self.isLandscape = !(self.view.frame.size.width == ([[UIScreen mainScreen] bounds].size.width * ([[UIScreen mainScreen] bounds].size.width < [[UIScreen mainScreen] bounds].size.height)) + ([[UIScreen mainScreen] bounds].size.height *([[UIScreen mainScreen] bounds].size.width > [[UIScreen mainScreen] bounds].size.height)));
NSLog(@"Update view height depending on orientation.");
//Update view height depending on orientation
if (self.isLandscape) {
//Readjust our keyboard's height
self.heightConstraint.constant = self.landscapeHeight;
[self.inputView addConstraint:self.heightConstraint];
self.view.frame = CGRectMake(0, 0, WIDTH(self.view), HEIGHT(self.view));
} else {
NSLog(@"Detected in portrait.");
//Re-adjust our keyboard's height
self.heightConstraint.constant = self.portraitHeight;
self.heightConstraint.priority = 990;
[self.inputView addConstraint:self.heightConstraint];
self.view.frame = CGRectMake(0, 0, WIDTH(self.view), HEIGHT(self.view));
}
}
- (void)viewWillAppear:(BOOL)animated {
NSLog(@"View Did Appear: Add Constraint.");
[self.view updateConstraints];
//Update our keyboard's height when view appears
self.heightConstraint = [NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:self.portraitHeight];
self.heightConstraint.priority = 990;
[self.view addConstraint:self.heightConstraint];
}
【问题讨论】:
-
我认为您覆盖
updateViewConstraints的方式肯定有问题。您是否在该方法中放置了断点以找出实际触发约束失败的行? -
糟糕!好主意,让我们试试吧!
-
好的,这很奇怪。所以显然
updateViewConstraints被调用了 3 次。在第一次通过时,它假定默认高度为 216,在第二次通过时,它调整所有内容,在第三次通过时,它加载最后一行(即使它在第二次空间中分配了空间)。奇怪的是,当我设置断点时,一切正常。所以也许这与时间有关?也许顶栏的约束会干扰我键盘上的默认约束? -
可能.. 如果您尝试在
updateConstraints而不是updateViewConstraints中调用这些函数会怎样? -
感谢 Remus,但我似乎找不到
updateConstraints的方法调用。嗯...
标签: objective-c ios8 autolayout nslayoutconstraint custom-keyboard