One of the benefits of SwiftUI is the ability to see a preview of the UI you are building in code. As you modifiy views, the preview to the side updates to reflect your changes.
When you first create an app, it comes with a simple preview as follows:
#Preview {
ContentView()
}
#Preview is a macro used to preview the view specified, in this case, the ContentView. It is the newer version of PreviewProvider, although that can still be used. An example of that will be shown later in this tutorial.
As you generate views, you’ll quickly run into problems with being unable to preview because ContentView, or another view, might require something to be passed in. In this tutorial, I’ll go through some basics to help you get the most out of the preview macro.
The Preview macro and other view tools allow you to quickly test different varients across different devices, ensuring that your UI works on devices of all sizes and dark/light mode settings.
[Read more…]