Skip to content

Instantly share code, notes, and snippets.

@amirdew
Created August 21, 2020 09:48
Show Gist options
  • Save amirdew/99aea5a44ff939396c7a5fcd90e22d42 to your computer and use it in GitHub Desktop.
Save amirdew/99aea5a44ff939396c7a5fcd90e22d42 to your computer and use it in GitHub Desktop.
An extention for making previews easier
// How to use:
struct YourView_Previews: PreviewProvider {
static var previews: some View {
YourView()
.makePreviews()
}
}
// Extensions:
extension View {
func makePreviews() -> some View {
Group {
ForEach(PreviewDevice.configs, id: \.name) {
previewDevice($0.device)
.previewDisplayName($0.name)
.environment(\.colorScheme, $0.colorScheme)
}
}
}
}
extension PreviewDevice {
static var configs: [(device: PreviewDevice, name: String, colorScheme: ColorScheme)] {
[
(PreviewDevice(rawValue: "iPhone 11 Pro"), "11 Pro", .dark),
(PreviewDevice(rawValue: "iPhone 11 Pro Max"), "11 Pro Max", .light),
(PreviewDevice(rawValue: "iPhone SE (2nd generation)"), "SE 2nd", .dark),
(PreviewDevice(rawValue: "iPod touch (7th generation)"), "iPod", .light)
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment