我有一个简单的 UIInputViewController 子类,只有两个重写方法。我将此输入 View Controller 用作我的 UIViewController 子类上的 inputAccessoryViewController
,它成为第一响应者。我尝试按照 Apple 文档的建议通过添加约束来指定 inputView 的高度。
问题是我的约束不起作用,添加约束时出现自动布局异常
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
...
(
"<NSLayoutConstraint:0x178aa1d0 V:[UIInputView:0x178a4ae0(0)]>",
"<NSLayoutConstraint:0x178b9520 V:[UIInputView:0x178a4ae0(500)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x178b9520 V:[UIInputView:0x178a4ae0(500)]>
我认为这意味着系统已经向输入 View 添加了零高度约束(因为它是用零高度创建的)。现在他们发生冲突,自动布局打破了我解决问题的约束。
当我尝试将它用作我的 View Controller 的 inputViewController
时(仅用于测试目的),我得到了相同的异常,但高度不是零,而是 216 像素。它还打破了我的限制,高度保持默认。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.inputView.translatesAutoresizingMaskIntoConstraints = NO;
self.inputView.backgroundColor = [UIColor redColor];
}
- (void)updateViewConstraints {
CGFloat _expandedHeight = 500;
NSLayoutConstraint *_heightConstraint =
[NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant: _expandedHeight];
[self.inputView addConstraint: _heightConstraint];
[super updateViewConstraints];
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.view setNeedsUpdateConstraints];
}
因此,我无法更改输入附件 View 高度。有人成功了吗?显然,Apple 文档没有提供任何帮助...
最佳答案
从 iOS 9.0 开始,这可以通过 inputView.allowsSelfSizing = YES;
关于ios - 无法更改 UIInputView 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26120043/