SwiftUI Mix and Match: A Seamless Blend of Old and New

KD Knowledge Diet
2 min readMay 30, 2024

--

With the introduction of SwiftUI, Apple opened new doors for app developers to create intuitive and beautiful user interfaces with less effort. However, the transition from UIKit/AppKit to SwiftUI doesn’t have to be an all-or-nothing endeavor. In this post, we’ll explore the power of mixing and matching SwiftUI with UIKit or AppKit, allowing you to adopt SwiftUI gradually and leverage its capabilities alongside your existing codebase.

Embracing SwiftUI in UIKit/AppKit:

1. Incorporating SwiftUI Hierarchies:
One of the most straightforward ways to start using SwiftUI within UIKit/AppKit projects is to embed SwiftUI views within your existing interfaces. SwiftUI views can be seamlessly integrated into your UIKit view controllers or AppKit views.

let vc = UIHostingController(rootView: HeaderView())

By creating a `UIHostingController` and specifying a SwiftUI view as its `rootView`, you can effortlessly incorporate SwiftUI into your existing UIKit-based UIs.

2. Retrofitting SwiftUI Support:
On the flip side, if you want to add SwiftUI support to your UIKit or AppKit-based views, you can create a wrapper that conforms to `UIViewRepresentable`. This wrapper acts as a bridge between UIKit or AppKit and SwiftUI, enabling you to update and display your existing views in SwiftUI style.

struct ProfileSwiftUIView: UIViewRepresentable {
let user: User
func makeUIView(context: Context) -> ProfileView {
return ProfileView()
}
func updateUIView(_ view: ProfileView, context: Context) {
view.nameLabel.text = user.name
view.imageView.image = user.image
}
}

By implementing `UIViewRepresentable`, you can bring UIKit or AppKit components into the world of SwiftUI effortlessly, combining the best of both worlds.

Benefits of Mixing and Matching:

1. Gradual Adoption: You don’t need to rewrite your entire interface in SwiftUI immediately. You can adopt SwiftUI piece by piece, enhancing specific parts of your app while keeping your existing UI intact.

2. Reuse Existing Code: By integrating SwiftUI with UIKit or AppKit, you can leverage your existing codebase and take advantage of SwiftUI’s features when and where it makes sense.

3. Smooth Transition: Transitioning to SwiftUI at your own pace ensures a smoother migration process. You can thoroughly test each SwiftUI component before fully committing to it.

4. Compatibility: Mixing SwiftUI with UIKit/AppKit allows you to maintain compatibility with older iOS/macOS versions that don’t support SwiftUI.

Conclusion:

SwiftUI empowers developers with a modern and intuitive way to build user interfaces. However, you don’t have to abandon your UIKit or AppKit projects to embrace it. Mixing and matching SwiftUI with UIKit or AppKit provides a flexible approach to adopting SwiftUI gradually, reusing existing code, and ensuring a seamless transition to the future of app development. So go ahead, blend the old with the new, and create remarkable user interfaces with SwiftUI in your UIKit/AppKit projects.

--

--

KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!