Integrating SwiftUI into a UIKit Project: A Step-by-Step Guide

Creating the iOS Project with Storyboard Interface

To begin, create a new UIKit project in Xcode and select “App” from the iOS tab. Then, choose “Storyboard” from the Interface dropdown. For this tutorial, we’ll remove the Storyboard and build the app’s UI programmatically.

Creating the SwiftUI Screen

Next, create a new SwiftUI View file by selecting “iOS” for the platform and “SwiftUI View” for the user interface. Give the file a name, and click Create. You should see the default SwiftUI View file with the text “Hello, World!” Update the SwiftUI screen by adding a simple UI design consisting of text and a button.

struct MySwiftUIScreen: View {
    var body: some View {
        VStack {
            Text("SwiftUI Screen")
               .padding()
            Button("Go back") {
                // Navigate back to UIKit screen
            }
           .padding()
        }
    }
}

Creating the UIKit View

Now, work with the UIKit’s ViewController to create a basic user interface. This will include a label with text “UIKit screen” and a button to navigate to the SwiftUI screen.

import UIKit

class MyUIKitViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let label = UILabel(frame: CGRect(x: 100, y: 100, width: 200, height: 20))
        label.text = "UIKit screen"
        view.addSubview(label)
        
        let button = UIButton(frame: CGRect(x: 100, y: 150, width: 200, height: 20))
        button.setTitle("Go to SwiftUI screen", for:.normal)
        button.addTarget(self, action: #selector(goToSwiftUIScreen), for:.touchUpInside)
        view.addSubview(button)
    }
    
    @objc func goToSwiftUIScreen() {
        // Present the SwiftUI screen
    }
}

Displaying the SwiftUI Screen

To present the SwiftUI screen, use the UIKit’s UIHostingController class. This class manages a SwiftUI view hierarchy and allows you to integrate SwiftUI into your UIKit project. Create a UIHostingController by passing your SwiftUI screen as a root view and the main navigation controller.

let swiftUIScreen = MySwiftUIScreen()
let hostingController = UIHostingController(rootView: swiftUIScreen)
navigationController?.pushViewController(hostingController, animated: true)

Navigating between UIKit and SwiftUI Screens

To navigate between the UIKit screen and the SwiftUI screen, use the UIHostingController to push the SwiftUI screen onto the navigation stack. To go back from the SwiftUI screen, use the UINavigationController to pop back to the UIKit screen.

// Navigate back to UIKit screen
navigationController?.popViewController(animated: true)

Final Project: UIKit App with SwiftUI Screen

The final version of the app includes a UIKit application with a SwiftUI screen. The tutorial demonstrates how to integrate SwiftUI into a UIKit project, allowing you to take advantage of SwiftUI’s benefits while still using UIKit.

Benefits of Integrating SwiftUI into a UIKit Project

By integrating SwiftUI into a UIKit project, you can take advantage of SwiftUI’s benefits, such as:

  • Declarative programming
  • Easy theme management
  • Live preview
  • Less prone to crashing

Additionally, integrating SwiftUI into a UIKit project allows you to use both frameworks in a single application, making it easier to migrate your app to SwiftUI in the future.

Leave a Reply