更改inputAccessoryView问题的高度

时间:2014-12-11 09:48:13

标签: ios ios8 inputaccessoryview

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

enter image description here

以下是一些代码段:

表视图控制器中的

- (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;
}
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];
    }
}
来自ios-helpers

的UIView类别中的

- (void)setHeight: (CGFloat)heigth {
    CGRect frame = self.frame;
    frame.size.height = heigth;
    self.frame = frame;
}

6 个答案:

答案 0 :(得分: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;
    }
}

在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];
    }];
}

那就是。

答案 1 :(得分:6)

在更改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;
        }
    }
}

答案 2 :(得分:3)

第一个答案并没有完全解决我的问题,但给了我一个巨大的暗示。 Apple确实为附件视图添加了一个私有约束,但是您无法在附件视图的约束列表中找到它。你必须从它的超级视图中搜索它。它杀了我几个小时。

答案 3 :(得分:2)

在阅读了上面的答案后,这是一个很好的发现,我担心依赖于您需要更改为[0]firstObject的约束是一个实施细节,可能会在我们之下发生变化未来。

在做了一些调试之后,我发现Apple附加输入视图上添加的约束似乎优先级为76.这是一个疯狂的低值,而不是{{{{ 1}}。

鉴于这个低priority值,它似乎是一个更简洁的解决方案,可以简单地有条件地添加/删除具有高优先级的另一个约束,比如说priority,当你想要调整视图大小时?

答案 4 :(得分:0)

试试这个: _vwForSendChat是输入附件视图 _txtViewChatMessage是输入附件视图中的textview

-(void)textViewDidChange:(UITextView *)textView {
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
if (newFrame.size.height < 40) {
    _vwForSendChat.frame = CGRectMake(0, 0, self.view.frame.size.width, 40);
} else {
    if (newFrame.size.height > 200) {
        _vwForSendChat.frame = CGRectMake(0, 0, self.view.frame.size.width, 200);
    } else {
       _vwForSendChat.frame = CGRectMake(0, 0, self.view.frame.size.width, newFrame.size.height);
    }
}
[self.txtViewChatMessage reloadInputViews];
}

答案 5 :(得分:0)

对于Xcode 11.2和Swift 5,此功能即使在动画块中也将更新inputAccessoryView约束

func updateInputContainerConstraints() {
    if let accessoryView = inputAccessoryView,
        let constraint = accessoryView.superview?.constraints.first(where: { $0.identifier == "accessoryHeight" }) {
        constraint.isActive = false
        accessoryView.layoutIfNeeded()
        constraint.constant = accessoryView.bounds.height
        constraint.isActive = true
        accessoryView.superview?.addConstraint(constraint)
        accessoryView.superview?.superview?.layoutIfNeeded()
    }
}
相关问题