【发布时间】:2020-05-24 14:41:16
【问题描述】:
要求:每当我输入 UITableViewHeaderFooterView 的 UITextView 并单击发送按钮时,我都想向 UITableView 添加一行。 UITableViewHeaderFooterView 必须是可重用的类。
我无法弄清楚在哪里添加 UITextViewDelegate 方法(无论是在自定义类本身还是在我使用它的主类中。)
我按照this 教程创建了一个自定义页脚类。
textViewDidChange 的逻辑
func textViewDidChange(_ textView: UITextView) {
let size = CGSize(width: view.frame.width - (sendButtonWidthConstraint.constant + textViewLeadingConstraint.constant), height: .infinity)
let estimatedSize = textView.sizeThatFits(size)
if estimatedSize.height >= self.textViewMaxHeight
{
textView.isScrollEnabled = true
}
else {
textViewHeightConstraint.constant = estimatedSize.height
containerViewHeightConstraint.constant = textViewHeightConstraint.constant + textViewBottomConstraint.constant + textViewTopConstraint.constant
sendButtonBottomConstraint.constant = (containerViewHeightConstraint.constant - sendButtonHeightConstraint.constant)/2
textView.isScrollEnabled = false
}
}
PS:基本上,我正在尝试开发 Instagram 的 cmets 功能。
【问题讨论】:
-
您可以将其中任何一个设置为 UITextViewDelegate。设置一个易于管理的位置。
-
我认为您需要将 ViewController 设置为 UITextView 委托。
标签: ios uitableview uitextview swift5