ios - 更改 TextView 高度 - iOS

标签 ios swift uitableview uitextview

我有一个 xib 文件作为 tableview 标题单元格,我希望它是自己的大小。在 xib 中,我有一个 TextView ,我想在按下按钮时更改它的高度。当按下 btn 时,textview 的高度应该是适合里面文本的大小,当再次按下 btn 时,它应该调整到只有两行文本。

enter image description here

我在我的 btn 操作中使用了这些代码行

    let contentSize = self.content.sizeThatFits(self.content.bounds.size)
    var frame = self.content.frame
    frame.size.height = contentSize.height
    self.content.frame = frame

它扩展了 textview 但 textview 重叠到它下面的元素。 使用下面的两行代码来调整标题的大小会使一切变得更糟,它只显示两行 textview 和 cm,类似的和其他的都消失了。

    tableView.estimatedSectionHeaderHeight = 115.0
    tableView.sectionHeaderHeight = UITableViewAutomaticDimension

我该如何解决这个问题?

最佳答案

可以使用 autolayout + UITableViewAutomaticDimension 来实现,检查一下: http://candycode.io/self-sizing-uitextview-in-a-uitableview-using-auto-layout-like-reminders-app/

关于ios - 更改 TextView 高度 - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38838300/

相关文章:

ios - CollectionView 和 UISegmentedControl 错误

ios - 在 swift 4.2 中交换数组值时检测到致命访问冲突

ios - 如何在WKWebView中使图像集全屏显示

ios - Swift,自定义 UITableViewCell 和 UIImageView。需要将单元格调整为图像高度

json - UITableView 不使用 SwiftyJSON 和 Alamofire 加载数据

iphone - UITableView tableHeaderView 缺少分隔符

ios - iOS 操作完成后,将按钮 "username"更改为文本字段

ios - 编程自动布局更改 UIView 的行为

ios - 在iOS的Xcode中,如何比较文本字段和两个字符串?

ios - 参数标签 '(_:)' 不匹配任何可用的重载