如何增加inputAccessoryView的高度

我已经花了几天时间才看到这个问题。

我有一个inputAccessoryView ,它包含一个包含inputAccessoryView和两个按钮的UIViewinputAccessoryView的行为符合预期,除了一个以外的所有情况下都能正常工作。

inputAccessoryView的高度增加时,我试图将inputAccessoryView的高度增加相同的量。 当我在inputAccessoryView中重新定义inputAccessoryView的高度时, inputAccessoryView在键盘上向下而不是向上增加高度。

我从SO那里尝试了许多不同的建议,但没有任何效果。 我想这是自动添加的NSLayoutConstraintinputAccessoryView但我不知道如何在swift和iOS 8.3中更改该值。

 func textViewDidChange(textView: UITextView) { var contentSize = messageTextView.sizeThatFits(CGSizeMake(messageTextView.frame.size.width, CGFloat.max)) inputAccessoryView.frame.size.height = contentSize.height + 16 } 

加入

 inputAccessoryView.setTranslatesAutoresizingMaskIntoConstraints(true) 

上面的代码帮助和inputAccessoryView高度正确向上增加但是我得到无法同时满足几个约束的约束,并且很难识别违规者。 另外,我得到了textView在新行的每个第二个实例上创建额外空间的奇怪效果。

谢谢。

要使输入附件视图垂直增长,只需​​设置其autoresizingMask = .FlexibleHeight ,计算其intrinsicContentSize并让框架执行其余操作。

代码:

 class InputAccessoryView: UIView, UITextViewDelegate { let textView = UITextView() override init(frame: CGRect) { super.init(frame: frame) // This is required to make the view grow vertically self.autoresizingMask = UIViewAutoresizing.FlexibleHeight // Setup textView as needed self.addSubview(self.textView) self.textView.translatesAutoresizingMaskIntoConstraints = false self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[textView]|", options: [], metrics: nil, views: ["textView": self.textView])) self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[textView]|", options: [], metrics: nil, views: ["textView": self.textView])) self.textView.delegate = self // Disabling textView scrolling prevents some undesired effects, // like incorrect contentOffset when adding new line, // and makes the textView behave similar to Apple's Messages app self.textView.scrollEnabled = false } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func intrinsicContentSize() -> CGSize { // Calculate intrinsicContentSize that will fit all the text let textSize = self.textView.sizeThatFits(CGSize(width: self.textView.bounds.width, height: CGFloat.max)) return CGSize(width: self.bounds.width, height: textSize.height) } // MARK: UITextViewDelegate func textViewDidChange(textView: UITextView) { // Re-calculate intrinsicContentSize when text changes self.invalidateIntrinsicContentSize() } }