Xcode Previews with UIKit and AppKit in Xcode 15
Table of Contents
In the past (before Xcode 15), there was no easy way to use Xcode Previews for non-SwiftUI components.
If you want to preview your UIView or UIViewController, you need to wrap it as SwiftUI view with UIViewRepresentable or UIViewControllerRepresentable.
In Xcode 15, we can easily create previews with new and more concise syntax using the #Preview
macro.
This new syntax support previewing for a wide range of view:
- SwiftUI
- UIKit (UIView and UIViewController)
- AppKit (NSView and NSViewController)
- Widget timeline providers
- Timeline entries
- Live activities
You can easily support sarunw.com by checking out this sponsor.
AI Grammar: Correct grammar, spell check, check punctuation, and parphrase.
How to preview UIKit and AppKit in Xcode 15
To preview any view with the new #Preview
macro, we just put the view you want to preview inside the macro.
#Preview {
Button("SwiftUI") {}
}
#Preview {
let button = UIButton(type: .system)
button.setTitle("UIKit", for: .normal)
return button
}
#Preview {
let button = NSButton(title: "AppKit", target: nil, action: nil)
return button
}
Read more article about Xcode, Xcode Previews, WWDC23, or see all available topic
Enjoy the read?
If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.
Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.
If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.
Become a patron Buy me a coffee Tweet ShareHow to preview UIView in Xcode Previews
If you use UIKit, you can preview your view using Interface Builder. But you can also preview a custom view that is created programmatically using Xcode Previews.
What's new in SF Symbols 5
In iOS 17, we can animate SF Symbols with the symbolEffect modifier.