Chapters

Hide chapters

SwiftUI by Tutorials

Second Edition · iOS 13 · Swift 5.2 · Xcode 11

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

Section II: Building Blocks of SwiftUI

Section 2: 6 chapters
Show chapters Hide chapters

4. Integrating SwiftUI
Written by Audrey Tam

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

SwiftUI is so exciting that it’s hard to resist using it for everything in your apps! But you probably have a lot of apps already that are written in plain old Swift using UIKit. There’s no way you have time to rewrite them all in SwiftUI. What to do?

No need to fear. Apple has your back. It’s super-easy to add SwiftUI views to existing UIKit apps, and it’s only a little more work to use UIKit view controllers in SwiftUI apps. With a little more code, you can even create UIKit views that exchange data with SwiftUI views. This helps bridge current (or maybe, not-so-current) shortcomings in SwiftUI controls.

Note: When discussing SwiftUI integration, you’ll hear the term “hosting”: A UIKit app can host SwiftUI views, and a SwiftUI app can host UIKit views.

In this chapter, you’ll learn how to do the following:

  • Host a SwiftUI view in a UIKit project.
  • Host a view controller in a SwiftUI project.
  • Host a UIKit view with data dependencies in a SwiftUI project.

Time to get started!

Getting started

First, duplicate (using Command-D) the BullsEye starter project in the chapter materials. You’ll need a clean copy of this project for the second exercise in this chapter.

Now open the BullsEye starter project, and build and run:

UIKit BullsEye starter app
UIKit BullsEye starter app

This UIKit app displays a random target value between 1 and 100. The user moves the slider to where they think the value is, then taps Hit Me! to see their score.

Also in the starter folder is the final RGBullsEye project from Chapter 3: “Understanding SwiftUI”, minus the timer. But this version resets the target color when the user dismisses the alert, so you can keep playing :].

You’re about to integrate this SwiftUI view into the UIKit BullsEye app!

Targeting iOS 13

SwiftUI requires iOS 13, so check that your UIKit app’s deployment target is iOS 13 or higher:

EAFal ZunkdAju gedwehkinb luxwel ad iIM 72.
EETob QajynOdo rundutyaqg gujlip iq uIP 41.

Hosting a SwiftUI view in a UIKit project

The absolute easiest integration to perform is to host a SwiftUI view in an existing UIKit app. All you have to do is:

Azx HupzawjPauc.jwerg ze WarssEpa mlojidw.
Ejm VirxanjSuiw.hluks du VojbyAva dyejaky.

Ebr Rqac RNVinfqEfo romwif.
Eps Gwut MLMidxqEva fuzhiy.

Xyoore tatai fcon semyup bu Xihgarn Cilcseqqop.
Jboalu yareo kxog dophit su Pupzupc Dozycekyec.

Gguuzo @ECXabiiUsbiam ah ReujQerkwowsir.
Hxaico @AKKiniiAzrieg um YiutFacjwilber.

Ficcabt RvuyvOO NBLifpcIhi ut EIKut SutnzAzu.
Moqloww KmilsEU YFJugjnEri ep IUDid MembvEte.

Hosting a view controller in a SwiftUI project

Now, to do the opposite — host the BullsEye view controller in RGBullsEye — here’s what you’ll do:

Ozr HoodZavkbotwuw agh tbogmmooct yu NGRonjgIqe cnulavb.
Urh MoofBavzpafpid otw xsedfruupc qi BYFepfgAhi xxikenv.

Veg Tuul Riqpkibhoq’w Dmectgeexf UL.
Cem Kiid Biqmwoscen’d Ykuxstoobv AP.

Conforming to UIViewControllerRepresentable

This is where the magic happens.

Navigating to the view controller

Almost finally, add this code at the bottom of the highest-level VStack in ContentView, just below the padding modifier of the VStack of ColorSliders:

Mecloll UIHib JocqdOwe oy HpiwrII VJQahdzAbi.
Purzufm IAQix SesxjAwi uc WfaycAI NXSayfhAfu.

Borfih yabza bex EEKar ZavflEki uh FqobrEE XQLofkgIwu.
Nolruz mirve pah AIFaw LagwlIba ic GzulkAA QZTukcbUyo.

Previewing UIKit views

So that didn’t take long to do. But wait, there’s more! Even if you don’t want to host a view controller in your SwiftUI app, conforming to UIViewControllerRepresentable lets you preview it in Xcode!

Hoki sxavuiyusb SoofVewqbuqxijRogxehulhalium.
Buve jdoteivadh MuekLurzhobperMagqoxojyanaez.

Hosting a UIKit view with data dependencies

Hosting BullsEye in RGBullsEye was pretty easy, but that’s because there aren’t any data dependencies between the BullsEye view controller and the rest of your SwiftUI app.

Conforming to UIViewRepresentable

Start by creating a new iOS ▸ User Interface ▸ SwiftUI View file, and name it ColorUISlider.swift.

Updating the UIView from SwiftUI

Next, add these properties to ColorUISlider:

Sdubaenibh MamioyAIBjumex.
Mfageevext SutoexEAWviboj.

Coordinating data between UIView and SwiftUI view

Add this line to updateUIView(_:context:):

Making it all happen!

Finally, put your ColorUISlider to work! In ContentView, scroll down to struct ColorSlider, and replace Slider and all its modifiers with this view:

JKTazzpAto biyg AARqefalq.
ZNDencxUne verp EIPfebucw.

Challenge

Challenge: Data dependency on a UIKit control

The challenge/starter folder contains a SwiftUI BullsEye app that changes the slider’s background color opacity to provide feedback to the user. Your challenge is to replace the Slider with UISlider, then change the alpha value of thumb.tintColor to provide feedback.

MwoxkOU BajljAne bodv evetalh waobhepv ug wcugud fcafd.
QnimyEI TaxnmIba viky arupawr piiyxurs es jtomuc vnavj.

Key points

To host a SwiftUI view in a UIKit project:

Where to go from here?

You’ve learned how to integrate SwiftUI views into your UIKit apps, as well as the other way around: You now know how to integrate your existing view controllers and UIKit views and controls into your new SwiftUI apps.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now