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

I use the following code to have a UITableViewCell dynamic height:

tableView.estimatedRowHeight = 155 tableView.rowHeight = UITableViewAutomaticDimension 

And this code so that the UITextView changes its 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 } } 

As soon as I change the text of the UITextView at runtime, the height of the UITextView changes, but the height of the UITableViewCell does not work. How can i change this? Help is much appreciated.

enter image description here enter image description here

+10
source share
6 answers

Get programmatically textview height ...

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

OR try this demo ...

Sizing a UITextView in a UITableView

https://damienpontifex.com/2014/10/01/self-sizing-uitableviewcell-with-uitextview-in-ios8/

+20
source

If you are using UITableViewAutomaticDimension , you must set sequential constraints from top to bottom. Without automatic shutdown or corresponding restrictions, the UITableViewAutomaticDimension will not work. Thus, you can cope with this task: set top,bottom,leading,trailing and fixed height restrictions on the textview . then open fixed height constraint and increase its constant , change or set the text view frame instead, and each will work, I think!

+8
source

I am improving Leo's answer.

If you are using UITableViewAutomaticDimension, then you must set sequential constraints from top to bottom. Without automatic placement or proper constraints, the UITableViewAutomaticDimension will not work. Thus, you can manage this thing like this: set upper, lower, leading, final and fixed height limits for your text view. Turn off the scroll function of UITextview, and you're done. You are not required to fix a height limit.

+4
source

You need to disable scrolling of the text view so that tableViewCell automatically resizes to fit the size of the contents of the textView.

+3
source

Step 1, disable scrolling TextView
Step 2, Add a constraint on top, left, right, and bottom
Step 3. Set the delegate for textView.
Step 4, Implementing the UITextViewDelegate Method Below

 - (void)textViewDidChange:(UITextView *)textView { [UIView setAnimationsEnabled:false]; [textView sizeToFit]; [self.tableView beginUpdates]; [self.tableView endUpdates]; [UIView setAnimationsEnabled:true];} 
0
source

Update code in Swift 4.2
Courtesy: http://www.swiftdevcenter.com/the-dynamic-height-of-uitextview-inside-uitableviewcell-swift/.
Create a custom cell called GrowingCell, add a UITextView to the cell and create an Outlet of this. Register this cell in a UITableView in your ViewController class. Your ViewController:

 import UIKit class ViewController: UIViewController { @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let nib = UINib(nibName: "GrowingCell", bundle: nil) self.tableView.register(nib, forCellReuseIdentifier: "GrowingCell") self.tableView.tableFooterView = UIView() self.tableView.dataSource = self } } extension ViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableView.automaticDimension } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "GrowingCell", for: indexPath) as! GrowingCell cell.cellDelegate = self return cell } } extension ViewController: GrowingCellProtocol { // Update height of UITextView based on string height func updateHeightOfRow(_ cell: GrowingCell, _ textView: UITextView) { let size = textView.bounds.size let newSize = tableView.sizeThatFits(CGSize(width: size.width, height: CGFloat.greatestFiniteMagnitude)) if size.height != newSize.height { UIView.setAnimationsEnabled(false) tableView?.beginUpdates() tableView?.endUpdates() UIView.setAnimationsEnabled(true) // Scoll up your textview if required if let thisIndexPath = tableView.indexPath(for: cell) { tableView.scrollToRow(at: thisIndexPath, at: .bottom, animated: false) } } } } 

Now go to the Custom GrowingCell cell and add the following code:

 import UIKit protocol GrowingCellProtocol: class { func updateHeightOfRow(_ cell: GrowingCell, _ textView: UITextView) } class GrowingCell: UITableViewCell { weak var cellDelegate: GrowingCellProtocol? @IBOutlet weak var textView: UITextView! override func awakeFromNib() { super.awakeFromNib() // Initialization code textView.delegate = self } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } } extension GrowingCell: UITextViewDelegate { func textViewDidChange(_ textView: UITextView) { if let deletate = cellDelegate { deletate.updateHeightOfRow(self, textView) } } } 

For more details: visit this full demo

0
source

All Articles