В других ответах не учитываются конфликтующие ограничения и вращение устройства. Этот ответ позволяет избежать ошибок, таких как «Невозможно одновременно удовлетворить ограничения» и проблемы, возникающие из-за этого. Он частично зависит от поведения, которое может измениться в будущих версиях iOS, но, по-видимому, это единственный способ решить эту проблему на iOS 8.
В вашем подклассе UIInputViewController
добавьте следующие методы:
- (void)updateViewConstraints {
[super updateViewConstraints];
// Update height when appearing
[self updateViewHeightConstraintIfNeeded];
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
// Update height when rotating
[self updateViewHeightConstraintIfNeeded];
}
- (void)updateViewHeightConstraintIfNeeded {
CGFloat preferedHeight = 216; // Portrait
if ( [UIScreen mainScreen].bounds.size.width
> [UIScreen mainScreen].bounds.size.height ) {
// Landscape
preferedHeight = 162;
}
NSLayoutConstraint *constraint = [self findViewHeightConstraint];
if ( preferedHeight != constraint.constant ) {
if ( constraint ) {
constraint.constant = preferedHeight;
} else {
// This is not run on current versions of iOS, but we add it to
// make sure the constraint exits
constraint = [NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0
constant:preferedHeight];
[self.view.superview addConstraint:constraint];
}
}
}
- (NSLayoutConstraint*)findViewHeightConstraint {
NSArray *constraints = self.view.superview.constraints;
for ( NSLayoutConstraint *constraint in constraints ) {
if ( constraint.firstItem == self.view
&& constraint.firstAttribute == NSLayoutAttributeHeight )
return constraint;
}
return nil;
}