Using previews in UIKit
If you love the ease with which you can preview UI changes, but you are still working on UIKit projects, rest easy, as you can also use the canvas preview feature while building UIKit apps. With Xcode 15, the preview macros work with UIKit out of the box. Under the hood, Xcode adds the boilerplate code to wrap UIKit views and view controllers in SwiftUI views, so they can view previewed in the canvas. In this recipe, we will learn how to preview a UIViewController
and a UIView
on the Xcode canvas.
Getting ready
Since this book is about SwiftUI and we are going to add previews to a UIKit app, we have already provided the UIKit app as a starter project, so you can focus on the preview functionality. It is very important that you clone or download the code for this chapter from GitHub, as explained in the Technical requirements section.
How to do it…
We will use the UIkit app provided and then add a couple of preview macros. The steps are as follows:
- Go...