iOS 8 Keyboard Extension: Constraint Error When Call Bar Appears?

911 Views Asked by At

I noticed that when I'm on the phone and the green call bar appears at the top of the screen, my custom keyboard won't appear (it's as if it gets skipped). So I debugged it and discovered that it's a constraint error.

Here is the error in the debugger:

*** 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

It's important to note that I never get any errors/warnings in either orientation or orientation change when there isn't a call bar. I am using the following to adjust my keyboard's height from 216 to 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));
    }
}

& here's my viewWillAppear:

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

** Note: WIDTH() and HEIGHT() are macros.

I am using auto layout in my xib. I'm not sure where to begin addressing this.

UPDATE

Thanks for helping debug this @Remus! I'm now trying to detect when the device is being rotated within my extension on iOS 8, but my updateConstraints method is still not being called.

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

There are 1 best solutions below

7
On BEST ANSWER

Let's try calling the functions inside of viewDidLayoutSubviews. This can also be called separately inside of any of the other constraint events (like updateViewConstraints, etc.).

- (void)viewDidLayoutSubviews {

    [super viewDidLayoutSubviews];
    [self setKeyboardConstraints];
}

- (void)setKeyboardConstraints {

    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));
    }
}