Structure your app for SwiftUI previews
Preview Multiple Files At Once
Pin previews by tapping the pin symbol in the preview canvas bottom bar.
Only one preview can be pinned.
By pinning a preview we can browse our project, edit files etc, and those previews will still be displayed on the screen.
App Life Cycle
It’s important to define explicit dependencies for our data.
Models that are created using
@StateObjectare not initialized in previews.
Where to define sample data
In the General tab of our project editor we have a Development Assets section:
it contains paths to files and folders that Xcode include only in the
developmentconfiguration of our appwe can add both assets and code (for preview sample data for example)
Structure Views To Be Previewable
For different ideas on how to do so
1. Immutable Inputs
Pass only basic types such as
IntandStringPass down to the view only data that the
Viewneeds.For buttons actions and similar, pass a closure.
2. Mutable Inputs (@Binding)
Use
.constant()for fixed@BindingpreviewsFor dynamic previews:
create an interemediate
View, that behaves as the@Statecontainerthe
bodyof this container will be the view that we would like to preview, injected with the container@State
3. Generics Inputs
Instead of passing actual explict types instances to a Views:
we can make our
Views genericwe can pass instances of a protocol that conforms to
ObservableObject,Identifiableand that provides anything that ourViewneeds
Use Environment
We can pass environment objects directly in the preview
Xcode Preview Toolbar
In Xcode 12 above each preview we have a new toolbar.
This toolbar displays the current state of the preview and the title in the middle and two buttons on both sides:
the leading button is used to either start the live preview or to attach the debugger to the preview.
the next button is used to send the preview to a real connected device
previewing on device will also display a
Previewsapp on the homescreen, which will open the last preview used in that phone, even when the phone is disconnected from the Xcode
the second trailing button lets set particular environment states and add padding to the preview
the last button creates a duplicate of the preview
