title: Why does SwiftUI UIHostingController have extra spacing?
body: <p>I'm trying to add a SwiftUI view to UIKit view using UIHostingController and it shows extra spacing(This sample is made to simulate an issue on a production app). Here is the screenshot.</p>
  <p><img src="https://i.stack.imgur.com/QGwjQ.png" alt="enter image description here" /></p>
  <p>Layout overview:</p>
  <pre><code>View
    UIStackView
      UIImageView
      UIView(red)
      UIHostingController
      UIView(blue)
  </code></pre>
  <p><strong>Issue:</strong>
  The swift UI view (UIHostingController) is shown between the red and blue views, it shows extra spacing after the divider. The spacing changes depending on the size of the SwiftUI view.</p>
  <p>If I reduce the number of rows (Hello World texts) or reduce the spacing, it seems working fine.</p>
  <p>Here is full source code(<a href="https://www.sendspace.com/file/ux0xt7" rel="noreferrer">https://www.sendspace.com/file/ux0xt7</a>):</p>
  <p>ViewController.swift(main view)</p>
  <pre><code>import UIKit
  
  class ViewController: UIViewController {
  
      @IBOutlet weak var mainStackView: UIStackView!
      
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view.
          
          addView()
      }
  
  
      private func addView() {
          mainStackView.spacing = 0
          mainStackView.alignment = .fill
          
          let imageView = UIImageView()
          imageView.image = UIImage(named: &quot;mountain&quot;)
          imageView.contentMode = .scaleAspectFill
          imageView.clipsToBounds = true
          imageView.heightAnchor.constraint(equalToConstant: 260).isActive = true
          mainStackView.addArrangedSubview(imageView)
          
          let redView = UIView()
          redView.backgroundColor = .red
          redView.heightAnchor.constraint(equalToConstant: 100).isActive = true
          mainStackView.addArrangedSubview(redView)
          
          
          
          let sampleVC = SampleViewController()
          //let size = sampleVC.view.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
          //sampleVC.view.heightAnchor.constraint(equalToConstant: size.height).isActive = true
          mainStackView.addArrangedSubview(sampleVC.view)
          
          
          let blueView = UIView()
          blueView.backgroundColor = .blue
          blueView.heightAnchor.constraint(equalToConstant: 100).isActive = true
          mainStackView.addArrangedSubview(blueView)
      }
  }
  </code></pre>
  <p>SampleView.swift</p>
  <pre><code>import SwiftUI
  
  struct SampleView: View {
      var body: some View {
          VStack(spacing: 0) {
              Text(&quot;Title&quot;)
  
              VStack (alignment: .leading, spacing: 30) {
                  Text(&quot;Hello World1&quot;)
                  Text(&quot;Hello World2&quot;)
                  Text(&quot;Hello World3&quot;)
                  Text(&quot;Hello World4&quot;)
                  Text(&quot;Hello World5&quot;)
                  Text(&quot;Hello World6&quot;)
                  Text(&quot;Hello World7&quot;)
                  Text(&quot;Hello World8&quot;)
                  Text(&quot;Hello World9&quot;)
                  Text(&quot;Hello World10&quot;)
              }
              Divider()
          }
          
      }
  }
  
  
  
  struct SampleView_Previews: PreviewProvider {
  
      
      static var previews: some View {
          Group {
              SampleView()
          }
      }
  }
  </code></pre>
  <p>SampleViewController.swift</p>
  <pre><code>import UIKit
  import SwiftUI
  
  class SampleViewController: UIViewController {
      override func viewDidLoad() {
          super.viewDidLoad()
          
          addView()
      }
      
      private func addView() {
          let hostingController = UIHostingController(rootView: SampleView())
          
          hostingController.view.backgroundColor = .clear
          hostingController.view.translatesAutoresizingMaskIntoConstraints = false
          addChild(hostingController)
          view.addSubview(hostingController.view)
          hostingController.didMove(toParent: self)
          
          NSLayoutConstraint.activate([
              hostingController.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
              hostingController.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
              hostingController.view.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0),
              hostingController.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
          ])
      }
  }
  </code></pre>
  <p>Thanks in advance!</p>