如何增加 inputAccessoryView 的高度
how can i increase the height of an inputAccessoryView
我已经在这上面花了好几天了,看不到解决办法。
我有一个 inputAccessoryView
,其中包含一个 UIView
,其中包含一个 textView
和两个按钮。 inputAccessoryView
的行为符合预期,并且在除一种情况外的所有情况下都可以正常工作。
当 textView 的高度增加时,我试图将 inputAccessoryView
的高度增加相同的量。当我在 textViewDidChange
中重新定义 inputAccessoryView
的高度时,inputAccessoryView
会在键盘上向下而不是向上增加高度。
我尝试了 SO 的许多不同建议,但没有任何效果。我想这是 inputAccessoryView
的自动添加的 NSLayoutConstraint
但我不知道如何在 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 在每两个新行的实例下面创建额外的 space。
谢谢。
要使输入附件视图垂直增长,您只需设置它的 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 = UIView.AutoresizingMask.flexibleHeight
// Setup textView as needed
self.addSubview(self.textView)
self.textView.translatesAutoresizingMaskIntoConstraints = false
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[textView]|", options: [], metrics: nil, views: ["textView": self.textView]))
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "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.isScrollEnabled = false
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var intrinsicContentSize: CGSize {
// Calculate intrinsicContentSize that will fit all the text
let textSize = self.textView.sizeThatFits(CGSize(width: self.textView.bounds.width, height: CGFloat.greatestFiniteMagnitude))
return CGSize(width: self.bounds.width, height: textSize.height)
}
// MARK: UITextViewDelegate
func textViewDidChange(_ textView: UITextView) {
// Re-calculate intrinsicContentSize when text changes
self.invalidateIntrinsicContentSize()
}
}
快进到 2020 年,您只需执行以下操作,其他一切与 maxkonovalov 的回答相同
override var intrinsicContentSize: CGSize {
return .zero
}
// MARK: UITextViewDelegate
func textViewDidChange(_ textView: UITextView) {
sizeToFit()
}
我已经在这上面花了好几天了,看不到解决办法。
我有一个 inputAccessoryView
,其中包含一个 UIView
,其中包含一个 textView
和两个按钮。 inputAccessoryView
的行为符合预期,并且在除一种情况外的所有情况下都可以正常工作。
当 textView 的高度增加时,我试图将 inputAccessoryView
的高度增加相同的量。当我在 textViewDidChange
中重新定义 inputAccessoryView
的高度时,inputAccessoryView
会在键盘上向下而不是向上增加高度。
我尝试了 SO 的许多不同建议,但没有任何效果。我想这是 inputAccessoryView
的自动添加的 NSLayoutConstraint
但我不知道如何在 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 在每两个新行的实例下面创建额外的 space。
谢谢。
要使输入附件视图垂直增长,您只需设置它的 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 = UIView.AutoresizingMask.flexibleHeight
// Setup textView as needed
self.addSubview(self.textView)
self.textView.translatesAutoresizingMaskIntoConstraints = false
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[textView]|", options: [], metrics: nil, views: ["textView": self.textView]))
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "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.isScrollEnabled = false
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var intrinsicContentSize: CGSize {
// Calculate intrinsicContentSize that will fit all the text
let textSize = self.textView.sizeThatFits(CGSize(width: self.textView.bounds.width, height: CGFloat.greatestFiniteMagnitude))
return CGSize(width: self.bounds.width, height: textSize.height)
}
// MARK: UITextViewDelegate
func textViewDidChange(_ textView: UITextView) {
// Re-calculate intrinsicContentSize when text changes
self.invalidateIntrinsicContentSize()
}
}
快进到 2020 年,您只需执行以下操作,其他一切与 maxkonovalov 的回答相同
override var intrinsicContentSize: CGSize {
return .zero
}
// MARK: UITextViewDelegate
func textViewDidChange(_ textView: UITextView) {
sizeToFit()
}