我注意到当我在打电话并且屏幕顶部出现绿色呼叫栏时,我的自定义键盘不会出现(就好像它被跳过了一样)。于是我调试了一下,发现是约束错误。
这是调试器中的错误:
*** 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];
}