I have (3) UITextViews, (1) UITextField and (1) UILabel within a single UICollectionViewCell. I have all the textViews changing in height when the text wraps onto a second line with a textViewDidChange function in an extension that conforms to the UITextViewDelegate protocol:
func textViewDidChange(_ textView: UITextView) {
let size = CGSize(width: frame.width, height: .infinity)
let estimatedSize = textView.sizeThatFits(size)
textView.constraints.forEach { (constraint) in
if constraint.firstAttribute == .height {
constraint.constant = estimatedSize.height
}
}
}
Ideally, whenever any of the textViews in my cell increase in height, I'd like that to increase the height of the cell.
I tried putting:
self.frame.size.height += estimatedSize.height
in the if constraint.firstAttribute == .height section, but that only increased the height of the cell until it reaches the bottom of the screen. And I'd like the user to be able to enter as much text as they'd like.
Is there a loop I should be using to edit the height of the cell whenever this textViewDidChange changes the height of a textView?
I'm super stumped, so any help would be awesome! Thanks in advance!
CAVEAT:
- After alot of searching, I found a bunch of answers that show how to dynamically increase the height of a cell. However, I don't know how to make unique cells within a
UICollectionView. If the most elegant solution here is to put all the textViews, the textField, and the label into unique cells, please show me how to do that!