UITextView not recalculating size

In my UITableViewCell subclass I have a UITextView and a UIImageView. The image covers the upper left corner of the UITextView and so I want to set an exclusion path so that the text wraps around the image. I'm doing it like this:

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  let choiceCell = cell as! ItemChoiceTableViewCell

  guard choiceCell.textView.textContainer.exclusionPaths.count == 0 else { return }

  var frame = choiceCell.displayImageView.bounds
  frame.size.height -= choiceCell.textView.frame.origin.y - choiceCell.displayImageView.frame.origin.y
  choiceCell.textView.textContainer.exclusionPaths = [UIBezierPath(rect: frame)]

  choiceCell.layoutSubviews()
}

The height of the cell is usually a line too short though, and I believe what is happening is that the necessary size isn't being properly recalculated after the exclusion path is added in. I've tried various things in place of

layoutSubviews
like
setNeedsLayout
and
setNeedsDisplay
but I can't get any of them to work properly.


How do I force the size of the UITextView to recalculate after the exclusion path is added?

Replies

Thanks for the pointer, Claude. Unfortunately that won't help because that's just asking the UITextView how tall it should be. This is in a cell using auto-layout to determine the cell size (which I forgot to mention), and when tableView(_:heightforRowAt:) is called the cell wouldn't yet have subviews setup, and so I wouldn't know the height of the image to set the exclusion path.