问题描述
当它内部的 inputAccessoryView
高度增加时,我正在尝试增加我的 UITextView
。
我偶然发现了这段代码:
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]|",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()
}
}
但我不知道如何添加它。运行它,在第 36 行运行错误。如果它仍然有效,我们如何添加此代码(当前使用故事板。UITextView
和 UIView
已连接到不同的文件)。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)