改变inputAccessoryView问题的高度

当我在iOS 8中更改inputAccessoryView的高度时,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; } 

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

在UIView类别从iOS辅助

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

最后,我find了答案。 在ios8中,苹果添加一个NSContentSizeLayoutConstraints到inputAccessoryView,并设置一个常数44.你不能删除这个常量,因为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]; }]; } 

那是。

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

第一个答案并没有完全解决我的问题,但给了我一个巨大的提示。 苹果公司并没有为附件视图添加私人约束,但在附件视图的约束列表中找不到它。 你必须从它的超级视图中search它。 它杀了我几个小时。

读完上面的答案后,我发现,依赖于你需要改变的约束是[0]还是firstObject是一个实现细节,在未来我们可能会改变。

在进行了一些debugging之后,我发现苹果在配件input视图中添加的约束条件似乎优先级为76.这是一个疯狂的低价值,而不是priority文件中列出的枚举之一。

考虑到这个低priority值,它似乎是一个更清洁的解决scheme,只是有条件地添加/删除具有高优先级的另一个约束,比如当你想调整视图的大小时,说UILayoutPriorityDefaultHigh

试试这个:_vwForSendChat是input附件视图_txtViewChatMessage是input附件视图内的文本视图

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