How to Use Live Previews in UIKit

Avoid endless Xcode builds on your UIKit projects

Finsi Ennes
Mar 2 · 2 min read

SwiftUI has changed the way of writing UI code but let’s be honest, most of iOS apps still use UIKit as framework. Migration to SwiftUI will be slow but meanwhile we can use some of its amazing tools, such as Live Previews.

In this tutorial you will learn how to wrap UIViewController and UIView into SwiftUI views in order to use Live Previews. Result will be the following:

Preview for UIViewController

If we want to add an UIViewController object into a SwiftUI interface, then we are going to need the UIViewControllerRepresentable protocol.

Start by creating a SwiftUI view that conforms such protocol:

Our struct will accept () -> UIViewController as closure:

  • This is a key aspect because it provides us a generic solution. We will be able to use any of our view controller instead a particular one.

Now, we have to add the requirements for UIViewControllerRepresentable:

  • The makeUIViewController method will return the initial view controller. SwiftUI calls this method once, this is when it’s ready to display the view. Then manages the view controller’s life cycle.
  • The updateUIViewController method is called for any changes affecting the corresponding. In our case, the view controller doesn’t depend on the state changes, so we leave it empty.

At this point we are ready to use the ViewControllerPreview. Thus, let’s implement a simple view controller to test this feature.

In this example, we are going to create a UIViewController that contains a centered image. We’ll retrieve several symbol images to show how Live Preview works on this context:

Create the PreviewViewController using the following code:

Finally, use the ViewControllerPreview inside a PreviewProvider:

We are done:

Preview for UIView

Once we know how to deal with UIViewController, work with UIView is really straightforward. Solution looks like:

The Startup

Get smarter at building your thing. Join The Startup’s +788K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Finsi Ennes

Written by

iOS Developer. Passionate about coding and UX design. For more content, you can follow me on Instagram @finsi.code

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Finsi Ennes

Written by

iOS Developer. Passionate about coding and UX design. For more content, you can follow me on Instagram @finsi.code

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store