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.
Layout overview:
View
UIStackView
UIImageView
UIView(red)
UIHostingController
UIView(blue)
Issue: 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.
If I reduce the number of rows (Hello World texts) or reduce the spacing, it seems working fine.
Here is full source code(https://www.sendspace.com/file/ux0xt7):
ViewController.swift(main view)
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: "mountain")
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)
}
}
SampleView.swift
import SwiftUI
struct SampleView: View {
var body: some View {
VStack(spacing: 0) {
Text("Title")
VStack (alignment: .leading, spacing: 30) {
Text("Hello World1")
Text("Hello World2")
Text("Hello World3")
Text("Hello World4")
Text("Hello World5")
Text("Hello World6")
Text("Hello World7")
Text("Hello World8")
Text("Hello World9")
Text("Hello World10")
}
Divider()
}
}
}
struct SampleView_Previews: PreviewProvider {
static var previews: some View {
Group {
SampleView()
}
}
}
SampleViewController.swift
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)
])
}
}
Thanks in advance!