title: How to override intrinsictContentSize for a view with flexible height and fixed width?
body: <p>I am trying to display custom views in a tableView and letting iOS compute the height of each row using <code>UITableViewAutomaticDimension</code>. Unfortunatelly my cell are not properly sized (bad height).</p>
  
  <p>My custom view is defined as followed (I am not using AutoLayout for this part, for many reasons, and I do not want to use it):</p>
  
  <pre><code>class MyView : UIView {
      var label: UILabel!
  
      override init(frame: CGRect) {
          super.init(frame: frame)
          setupView()
      }
  
      required init?(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)
          setupView()
      }
  
      func setupView() {
          label = UILabel()
          label.numberOfLines = 0
          addSubview(label)
      }
  
      func setText(text: String) {
          label.text = text
          setNeedsLayout()
      }
  
      override func layoutSubviews() {
          super.layoutSubviews()
          label.frame = bounds
          invalidateIntrinsicContentSize()
      }
  
      override var intrinsicContentSize: CGSize {
          guard let text = label.text else { return .zero }
          let width = bounds.width
          let height = text.heightWithConstrainedWidth(width: width, font: label.font)
          return CGSize(width: width, height: height)
      }
  }
  </code></pre>
  
  <p>Now, I wrapped this custom view in a <code>UITableViewCell</code> to use it in a <code>UITableView</code>. Here I am using AutoLayout:</p>
  
  <pre><code>class MyCell : UITableViewCell {
      var myView: MyView!
  
      override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
          super.init(style: style, reuseIdentifier: reuseIdentifier)
          setupCell()
      }
  
      required init?(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)
          setupCell()
      }
  
      func setupCell() {
          myView = MyView()
          contentView.addSubview(myView)
          myView.snp.makeConstraints { make in
              make.edges.equalTo(contentView)
          }
      }
  }
  </code></pre>
  
  <p>Now, using the following viewController, I display a cell with a big text to see if its height gets automatically computed:</p>
  
  <pre><code>class ViewController: UIViewController {
      var tableView: UITableView!
  
      override func viewDidAppear(_ animated: Bool) {
          super.viewDidAppear(animated)
          tableView = UITableView()
          view.addSubview(tableView)
          tableView.snp.makeConstraints { make in
              make.edges.equalTo(view)
          }
          tableView.register(MyCell.self, forCellReuseIdentifier: "MyCell")
          tableView.rowHeight = UITableViewAutomaticDimension
          tableView.estimatedRowHeight = 30
          tableView.dataSource = self
      }
  }
  
  extension ViewController: UITableViewDataSource {
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return 1
      }
  
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyCell
          cell.myView.setText(text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur")
          return cell
      }
  }
  </code></pre>
  
  <p>Unfortunatelly, the cell's height is equal to 21. And this, even though the height returned by the last <code>intrinsicContentSize</code> call of <code>MyView</code> is 143. Here is a screenshot of the result:
  <a href="https://i.stack.imgur.com/I8aM0t.png" rel="noreferrer"><img src="https://i.stack.imgur.com/I8aM0t.png" alt="enter image description here"></a></p>
  
  <p>The cell is sized correctly if it is recycled (I scroll the cell outside the screen):
  <a href="https://i.stack.imgur.com/vrAAJt.png" rel="noreferrer"><img src="https://i.stack.imgur.com/vrAAJt.png" alt="enter image description here"></a></p>
  
  <p>I know that the <code>intrinsictContentSize</code> of <code>MyView</code> uses its frame and it should not, but I have no idea on how to design a UIView subclass that has a flexible height given a fixed width. I thought it would work given the fact I call <code>invalidateIntrinsicContentSize()</code> in MyView's <code>layoutSubviews()</code>.</p>
  
  <p>How can I change this code to have the good cell's height from start?</p>