我有一个UITableView,每个单元格内有两个UITextViews . 我希望UITableViewCell和UITextView的高度都增加,这样用户就不需要在UITextView中滚动了 . 这是我尝试过的:
在TableViewController类中:
self.tableView.estimatedRowHeight = 44
self.tableView.rowHeight = UITableViewAutomaticDimension
在TableViewCell类中(从here获取):
func textViewDidChange(textView: UITextView) {
var frame : CGRect = textView.frame
frame.size.height = textView.contentSize.height
textView.frame = frame
}
当用户输入超出UITextView的设置宽度时,UITableView将高度从44增加到大约100,UITextView的高度不会增加 . 我设置了约束,以便UITextView的高度等于UITableViewCell的高度 .
任何想法为什么会发生这种情况和 how to correctly dynamically change the UITextView and UITableView's heights?
2 回答
我的回答是基于我们在社交应用程序生成中使用的内容Impether,因为您在Twitter上告诉我您使用了该应用程序并且您在那里看到了扩展UITextView .
首先,我们有一个包含
UITextView
的自定义UITableViewCell类,它将被展开(此类还有一个相应的xib文件,你可以自己设计):定义了自定义单元格后,您可以在基于
UITableViewController
的类中使用它,如下所示:首先确保您的自动布局约束不起作用)然后尝试将
frame
更改为bounds
. 一个视图's frame (CGRect) is the position of its rectangle in the superview'的坐标系 . 在我的经验中,使用frame
可能会导致奇怪的问题 .