1

I have an app that uses storyboard, I need one of the tabs to be SwiftUI, how can I add it to my existing project?

1 Answer 1

1

I assume someone will need to know this at some point,

step 1. Add a Hosting View Controller to your storyboard

step 2. Create a root view controller relationship segue between your navigation controller/tab bar controller and the HostingView Controller

step 3. create your SwiftUI Class

import SwiftUI

struct AnalyticsView: View {
    var body: some View {
        Text("Hello")
    }
    
}

struct AnalyticsView_Previews: PreviewProvider {
    static var previews: some View {
        ("Hello World")
    }
}

step 4. create a UIHostingController class and set the HostingViewController to that class in the class inspector

import UIKit
import SwiftUI

class AnalyticsVC: UIHostingController<AnalyticsView> {

    required init?(coder aDecoder: NSCoder){
        super.init(coder: aDecoder, rootView: AnalyticsView())
    }
}

step 5. build your view controller from your SwiftUI view

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.