2

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

enter image description here

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

1 Answer 1

3

You’re applying your .frame after you apply your .background.

This means that the background view is applied to the area of the view as determined by its contents, and when the view’s frame is expanded the background stays the same size.

Try switching the order of the modifiers so that the frame size is readjusted before the background is applied.

.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(.ultraThinMaterial, in: Rectangle())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.