如何增加inputAccessoryView的高度 [英] how can i increase the height of an inputAccessoryView
问题描述
我已经花了几天时间没有解决方案。
I have spent several days on this with no solution in sight.
我有一个 inputAccessoryView
,它包含一个 UIView
包含 textView
和两个按钮。 inputAccessoryView
的行为符合预期,除了一个以外的所有情况都能正常工作。
I have an inputAccessoryView
which consists of a UIView
containing a textView
and two buttons. The behaviour of the inputAccessoryView
is as expected and works fine in all cases except one.
当textView的高度增加时,我试图以相同的数量增加 inputAccessoryView
的高度。当我在 textViewDidChange
中重新定义 inputAccessoryView
的高度时, inputAccessoryView
在键盘上向下增加高度而不是向上。
When the height of the textView increases, I am trying to increase the height of the inputAccessoryView
by the same amount. When I redefine the height of the inputAccessoryView
in textViewDidChange
, the inputAccessoryView
increases height downwards over the keyboard instead of upwards.
我尝试了许多不同的建议,但没有任何效果。我想这是 inputAccessoryView
的自动添加 NSLayoutConstraint
,但我不知道如何在swift中更改该值iOS 8.3。
I have tried many different suggestions from SO but nothing has worked. I guess it is the automatically added NSLayoutConstraint
of the inputAccessoryView
but I have no idea how to change that value in swift and 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高度正确向上增加但是我得到了Unable同时满足若干约束的约束,并且很难识别违法者。另外,我得到了textView在新行的每个第二个实例上创建额外空间的奇怪效果。
to the above code helps and the inputAccessoryView height increases upwards correctly however I get Unable to simultaneously satisfy constraints for several constraints and it is very difficult to identify the offenders. Also I get an odd effect of the textView creating extra space below on every second instance of a new line.
谢谢。
推荐答案
要使输入附件视图垂直增长,只需设置 autoresizingMask = .FlexibleHeight
,计算其 intrinsicContentSize
让框架完成剩下的工作。
To make input accessory view grow vertically you just set its autoresizingMask = .FlexibleHeight
, calculate its intrinsicContentSize
and let the framework do the rest.
代码:
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()
}
}
这篇关于如何增加inputAccessoryView的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!