title: UIHostingController should expand to fit contents
body: <p>I have a custom <code>UIViewControllerRepresentable</code> (layout-related code shown below). This tries to replicate the native SwiftUI <code>ScrollView</code>, except it scrolls from the bottom except the top.</p>
  <h3>View hierarchy</h3>
  <pre><code>view: UIView
  |
  \- scrollView: UIScrollView
     |
     \- innerView: UIView
        |
        \- hostingController.view: SwiftUI hosting view
  </code></pre>
  <p>This all works as intended when the view is initialized. The hosting view is populated with its contents, and the constraints make sure that the scroll view's <code>contentSize</code> is set properly.</p>
  <p>However, when the contents of the hosting view changes, the <code>hostingController.view</code> doesn't resize to fit its contents.</p>
  <p><a href="https://i.stack.imgur.com/oAk9F.png" rel="noreferrer"><img src="https://i.stack.imgur.com/oAk9F.png" alt="Screenshot of UI capture from app in Xcode. Shows contents of hosting controller expanding behind the bounds of the hosting view itself, without properly resizing." /></a></p>
  <blockquote>
  <p>Green: As intended, the scroll view matches the size of the hosting view controller.</p>
  <p>Blue: The hosting view itself. It keeps the size it had when it was first loaded, and doesn't expend as it should.</p>
  <p>Red: A stack view within the hosting view. In this screenshot, content was been added to the stack, causing it to expand. You can see the difference in size as a result.</p>
  </blockquote>
  <h3>The UIHostingController (blue) should expand to fit its contents (red).</h3>
  <p>The scroll view's content size is <em>not</em> explicitly set, because this is handled by auto layout.</p>
  <p>Constraint code is shown below, if it helps.</p>
  <pre><code>class UIBottomScrollViewController&lt;Content: View&gt;: UIViewController, UIScrollViewDelegate {
      var hostingController: UIHostingController&lt;Content&gt;! = nil
  
      init(rootView: Content) {
          self.hostingController = UIHostingController&lt;Content&gt;(rootView: rootView)
          super.init(nibName: nil, bundle: nil)
      }
      required init?(coder: NSCoder) {
          fatalError(&quot;init(coder:) has not been implemented&quot;)
      }
  
      var scrollView: UIScrollView = UIScrollView()
      var innerView = UIView()
  
      override func loadView() {
          self.view = UIView()
          self.addChild(hostingController)
          view.addSubview(scrollView)
          scrollView.addSubview(innerView)
          innerView.addSubview(hostingController.view)
  
          scrollView.delegate = self
          scrollView.scrollsToTop = true
          scrollView.isScrollEnabled = true
          scrollView.clipsToBounds = false
  
          scrollView.layoutMargins = .zero
          scrollView.preservesSuperviewLayoutMargins = true
  
          scrollView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
          scrollView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
          scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
          scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
  
          innerView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
          innerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
          innerView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
          innerView.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
          innerView.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true
          innerView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
  
  
          hostingController.view.topAnchor.constraint(equalTo: innerView.topAnchor).isActive = true
          hostingController.view.leftAnchor.constraint(equalTo: innerView.leftAnchor).isActive = true
          hostingController.view.rightAnchor.constraint(equalTo: innerView.rightAnchor).isActive = true
          hostingController.view.bottomAnchor.constraint(equalTo: innerView.bottomAnchor).isActive = true
  
  
          hostingController.view.autoresizingMask = []
          hostingController.view.layoutMargins = .zero
          hostingController.view.insetsLayoutMarginsFromSafeArea = false
          hostingController.view.translatesAutoresizingMaskIntoConstraints = false
  
          scrollView.autoresizingMask = []
          scrollView.layoutMargins = .zero
          scrollView.insetsLayoutMarginsFromSafeArea = false
          scrollView.translatesAutoresizingMaskIntoConstraints = false
  
          innerView.autoresizingMask = []
          innerView.layoutMargins = .zero
          innerView.insetsLayoutMarginsFromSafeArea = false
          innerView.translatesAutoresizingMaskIntoConstraints = false
  
          hostingController.didMove(toParent: self)
  
          scrollView.keyboardDismissMode = .interactive
      }
  }
  
  struct BottomScrollView&lt;Content: View&gt;: UIViewControllerRepresentable {
      var content: () -&gt; Content
  
      init(@ViewBuilder content: @escaping () -&gt; Content) {
          self.content = content
      }
  
      func makeUIViewController(context: Context) -&gt; UIBottomScrollViewController&lt;Content&gt; {
          let vc = UIBottomScrollViewController(rootView: self.content())
          return vc
      }
      func updateUIViewController(_ viewController: UIBottomScrollViewController&lt;Content&gt;, context: Context) {
          viewController.hostingController.rootView = self.content()
      }
  }
  </code></pre>