iOS Dev
iOS Dev

Reputation: 4248

Prevent calling of viewDidLayoutSubviews on user interaction with iOS 8 custom keyboard

I'm developing an iOS 8 custom keyboard and I'm facing the following issue:

- (void) viewDidLayoutSubviews{} method from UIInputViewController subclass is called each time user touches the keyboard (tap, swipe etc.). I would like to avoid this, there is no need to call it when user touches the keyboard.

Also I found that if I comment the following lines, viewDidLayoutSubviews is not called anymore when user interacts with keyboard:

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

But I can't get rid of these constraints, because I need them to change keyboard's height. How could I solve this?

Upvotes: 1

Views: 1842

Answers (1)

ljk321
ljk321

Reputation: 16790

I highly recommend you not to use viewDidLayoutSubviews(and also viewWillLayoutSubviews) in keyboard extension. They will be called due to frame changing, constraints changing and so many other events, which will sometimes cause unexpected problems. viewDidAppear can be a replacement.

If you really have to override viewDidLayoutSubviews, try use flags to prevent it from getting incorrectly called.

-(void)viewDidLayoutSubviews {
    if (self.alreadyLoaded) {
        return;
    }
    else {
    //...
    }
}

Upvotes: 1

Related Questions