iTranslated by AI
The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📚
Handy Tips for SwiftUI Previews
SwiftUI is convenient because you can quickly check the display from the Preview.
The following tips make it even more convenient.
Checking Light Mode and Dark Mode simultaneously
You can switch the color scheme (theme) by specifying preferredColorScheme().
By writing as follows, you can display both Light and Dark modes at the same time.
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Group {
ContentView().preferredColorScheme(.light)
ContentView().preferredColorScheme(.dark)
}
}
}
Displaying according to View size
By specifying sizeThatFits in previewLayout(), the preview will be displayed according to the size of the View.
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Group {
ContentView().preferredColorScheme(.light).previewLayout(.sizeThatFits)
ContentView().preferredColorScheme(.dark).previewLayout(.sizeThatFits)
}
}
}
Discussion