Use SwiftUI Previews in UIKit

ViewControllers need to be final

Steven Curtis
2 min readNov 25, 2021

Wouldn’t it be wonderful if you could take a UIView or a UIViewController and use SwiftUI’s previews to be able to have an interactive preview for it?

Guess what: You can.

The UIViewController

Hint: your UIViewController needs to be marked as final (so you can’t use inheritance here — but should you be doing so in any case?)

We take my usual way of creating a UIViewController programatically and create something like the following.

That looks like it doesn’t do anything — but I’ve put an image on the nib file for this particular example (of course you could place any image you would like onto your nib if you’re following along at home).

Conform to UIViewControllerRepresentable

--

--