ios - Swift - 根据 UITextView 布局约束的 UITableViewCell 高度

标签 ios swift

在我的表格 View 单元格中有 3 个对象; ImageView 、标签和 TextView 。

这就是我设置 textView 及其约束的方式:

textView.translatesAutoresizingMaskIntoConstraints = false
textView.textColor = UIColor.blue
textView.font = UIFont.systemFont(ofSize:15.0)
textView.textAlignment = NSTextAlignment.left
textView.isScrollEnabled = false

**注意,self 是一个包含textView 的UIView。 TableView 单元格的内容 View 将包含此 UIView

self.addConstraints([
            textView.topAnchor.constraint(equalTo: label.bottomAnchor, constant: 10.0),
            textView.leftAnchor.constraint(equalTo: imageView.rightAnchor, constant: 10.0),
            textView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -10.0),
            textView.heightAnchor.constraint(greaterThanOrEqualToConstant: 20.0)
            ])

我设置了isScrollEnabled = false,以便textView的高度根据其内容计算

对于我的 UITableView,我这样做了:

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

使用上面的代码,我的 tableviewcell 没有显示正确的高度。我相信这是因为我的 textview 高度约束给了 tableviewcell 内容 View 错误的高度。

我该如何解决这个问题?

最佳答案

  1. 设置isScrollEnabled = false不会影响textView高度的计算。
  2. 为了根据 textView 的内容调整单元格大小,除了 top(=) 之外,您还应该向 textView 添加 bottom(=) 约束> 和可选高度(>=)

关于ios - Swift - 根据 UITextView 布局约束的 UITableViewCell 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40366937/

相关文章:

ios - UILocalNotification 声音不在设备上播放

objective-c - iOS GLKit 上连续滚动 Sprite Sprite 之间的间隙

ios - 将按钮添加到 AVPlayerViewController 的顶部或底部栏

swift - NSOpenPanel使用详解

string - 如何使用 Swift 修改字符串中的单个字符?

javascript - 慢速键盘/选择器在 cordova 4 iOS 中响应

ios - 将图像上传到服务器 Swift 3

ios - 无法在重构的 Storyboard上设置标签栏角标(Badge)值

ios - 在 didTapMarker 中进行 API 调用以获取标记标题并在 Google Maps API for iOS 中收到响应后分配它

ios - 代码滚动到 TableView 中的下一行?