I have a SwiftUI view that needs to be shown in a UIKit View Controller. I'm using UIHostingController to embed my SwiftUI view.
I'm trying to figure out how can my SwifUI view expands its size to match UIHostingController's frame. My UIHostingController currently has the same frame as backgroundImageView of the the ViewController but my FeatureIntroductionView does not expand to fit in UIHostingController
This is my SwiftUI view
struct FeatureIntroductionView: View {
let image: String
let title: String
let subtitle: String
let buttonTitle: String
var body: some View {
VStack(spacing: 33) {
Image(image)
VStack(alignment: .center, spacing: 4) {
Text(title)
.foregroundColor(.white)
.font(Font(UIFont.boldFontWith(size: 28)))
Text(subtitle)
.foregroundColor(.white)
.font(Font(UIFont.regularFontWith(size: 16)))
.multilineTextAlignment(.center)
}
SecondaryButton(title: buttonTitle) {
// Close
}
}
.padding(48)
.background(.ultraThinMaterial,
in: Rectangle())
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
I'm embedding it like below in my UIViewController
func addIntroductoryView() {
let swipeFeatureIntroductionView = FeatureIntroductionView(image: "Swipe",
title: "Swipe to change",
subtitle: "Switch between quotes by swiping gesture or tapping on the screen.",
buttonTitle: "Got it")
var swipeFeatureIntroductionHostingController = UIHostingController(rootView: swipeFeatureIntroductionView)
swipeFeatureIntroductionHostingController.view.invalidateIntrinsicContentSize()
addChild(swipeFeatureIntroductionHostingController)
swipeFeatureIntroductionHostingController.view.frame = backgroundImageView.frame
swipeFeatureIntroductionHostingController.view.layer.cornerRadius = 16
swipeFeatureIntroductionHostingController.view?.clipsToBounds = true
swipeFeatureIntroductionHostingController.view.backgroundColor = .clear
view.addSubview(swipeFeatureIntroductionHostingController.view)
swipeFeatureIntroductionHostingController.didMove(toParent: self)
}
I'm looking for a way to expand FeatureIntroductionView to fill the same space as backgrodun image.
Thanks
