ios - 无法更改 UIInputView 高度

标签 ios objective-c ios8 autolayout uiinputviewcontroller

我有一个简单的 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/

相关文章:

ios - 为什么 self.splitViewController == nil?

ios - 以编程方式向单元格添加单独数量的按钮

android - sagepay 是否有适用于 iOS 和/或 Android 的应用内支付的任何库或 SDK?

objective-c - 使用字符串而不是对象调用工厂方法

ios - 是否可以使用 UITableViewStylePlain 禁用 UITableView 中的 float 标题?

iOS错误: Thread 1:EXC_BAD_ACCESS(code=1,地址=0x726f635f)?

swift - 不正确地使用 Core Graphics

ios - UINavigationController pushViewController 推送 Controller 然后 "automatically"关闭它

ios - ionic 3 : Deeplink using Universal Link on iOS not opening app

ios - 使用 Segue 从 UIViewController 传输字符串