Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift / how to use UITextView with dynamic height INSIDE UITableViewCell with dynamic height

I'm using the following code to have the UITableViewCell have a dynamic height:

tableView.estimatedRowHeight = 155
tableView.rowHeight = UITableViewAutomaticDimension

And this code to have the UITextView change it's height based on the content.

extension NewUserTweetTableVC: UITextViewDelegate {

    func textViewDidChange(textView: UITextView) {
        let fixedWidth = textView.frame.size.width
        textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
        let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
        var newFrame = textView.frame
        newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
        textView.frame = newFrame    
    }
}

Once I change the text of the UITextView during runtime, the height of the UITextView changes, but the height of the UITableViewCell does not. How can I change that? Help is very appreciated.

enter image description here enter image description here

like image 473
David Seek Avatar asked Sep 14 '16 07:09

David Seek


1 Answers

Get programmatically height of textview...

let textView = UITextView()//your Text view
let sizeThatShouldFitTheContent = textView.sizeThatFits(textView.frame.size)
let height = sizeThatShouldFitTheContent.height

OR try this demo...

Self-sizing UITextView in a UITableView

https://www.damienpontifex.com/posts/self-sizing-uitableviewcell-with-uitextview-in-ios8/

like image 118
Rahul Mayani Avatar answered Oct 07 '22 02:10

Rahul Mayani