更改inputAccessoryView问题的高度

Yij*_*jun 17 ios inputaccessoryview ios8

当我更改inputAccessoryViewiOS 8中的高度时,inputAccessoryView不会转到右侧原点,而是覆盖键盘.

在此输入图像描述

以下是一些代码段:

在表视图控制器中

- (UIView *)inputAccessoryView {
    if (!_commentInputView) {
        _commentInputView = [[CommentInputView alloc] initWithFrame:CGRectMake(0, 0, [self width], 41)];
        [_commentInputView setPlaceholder:NSLocalizedString(@"Comment", nil) andButtonTitle:NSLocalizedString(@"Send", nil)];
        [_commentInputView setBackgroundColor:[UIColor whiteColor]];
        _commentInputView.hidden = YES;
        _commentInputView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
    }

    return _commentInputView;
}
Run Code Online (Sandbox Code Playgroud)

在CommentInputView中

#when the textview change height
- (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(float)height {
    if (height > _textView_height) {
        [self setHeight:(CGRectGetHeight(self.frame) + height - _textView_height)];
        [self reloadInputViews];
    }
}
Run Code Online (Sandbox Code Playgroud)

来自ios-helpers的 UIView类别

- (void)setHeight: (CGFloat)heigth {
    CGRect frame = self.frame;
    frame.size.height = heigth;
    self.frame = frame;
}
Run Code Online (Sandbox Code Playgroud)

Yij*_*jun 19

最后,我找到了答案.在ios8中,apple将一个NSContentSizeLayoutConstraints添加到inputAccessoryView并用44.设置一个常量.你不能删除这个constaint,因为ios8用它来计算inputAccessoryView的高度.所以,唯一的解决方案是改变这个常数的值.

在ViewDidAppear中

- (void)viewDidAppear:(BOOL)animated {
    if ([self.inputAccessoryView constraints].count > 0) {
        NSLayoutConstraint *constraint = [[self.inputAccessoryView constraints] objectAtIndex:0];
        constraint.constant = CommentInputViewBeginHeight;
    }
}
Run Code Online (Sandbox Code Playgroud)

在textview高度更改时更改inputAccessoryView高度

- (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(float)height {

    NSLayoutConstraint *constraint = [[self constraints] objectAtIndex:0];
    float new_height = height + _textView_vertical_gap*2;

    [UIView animateWithDuration:0.2 animations:^{
        constraint.constant = new_height;
    } completion:^(BOOL finished) {
        [self setHeight:new_height];
        [self reloadInputViews];
    }];
}
Run Code Online (Sandbox Code Playgroud)

那是.


stu*_*stu 7

在更改inputAccessoryView的高度时,可以更新Yijun答案中提到的约束的一种方法是在inputAccessoryView上覆盖setFrame :. 这不依赖于高度约束是数组中的第一个.

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];

    for (NSLayoutConstraint *constraint in self.constraints) {
        if (constraint.firstAttribute == NSLayoutAttributeHeight) {
            constraint.constant = frame.size.height;
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)