Hello!

The question is this. Increasing the size of the textView using this code:

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

The text is entered without problems, the cell is enlarged, BUT, the text is for some reason invisible and it becomes visible after turning the screen in any direction, it would be useful if I had an application about secret correspondence and I would have done this feature.

With what it can be connected? And what happens when you rotate the screen? [ enter image description here enter image description here

enter image description here

  • Do you use autolayout? Most likely, when you rotate the screen, the size of the constraints changes and the textView becomes larger. You need to make the priority one and constraints a little smaller so that it can change depending on the size of the textView. You can also try updating manually with setNeedsLayout / layoutIfNeeded. But this does not help if the constraints are not configured correctly. - Vitali Eller
  • @VitaliEller why then when you rotate the screen to its original state, we see a stretched cell? - Alexander Govorukhin
  • Because at the first turn, the system already knows the required frame, it has drawn it, and when it is turned again, no changes occur, the frame remains the same. Try to write a lot of text, turn the phone, see this text, delete it, turn it back, and try to write it again. In theory, the problem should be repeated. - Vitali Eller
  • @VitaliEller, perhaps, but I changed the textViewDidChange method to another code and the cell is stretched as needed. - Alexander Govorukhin
  • Then add the correct answer so that others who have encountered the same problem know how to solve it. - Vitali Eller

1 answer 1

The solution to this problem:

 func textViewDidChange(_ textView: UITextView) { let size = textView.bounds.size let newSize = textView.sizeThatFits(CGSize(width: size.width, height: CGFloat.greatestFiniteMagnitude)) if size.height != newSize.height { UIView.setAnimationsEnabled(false) tableView?.beginUpdates() tableView?.endUpdates() UIView.setAnimationsEnabled(true) } }