Skip to content

Instantly share code, notes, and snippets.

@kvdesa
Last active July 14, 2022 19:46
Show Gist options
  • Save kvdesa/e7390272e75eca2080590e4fc54ff42c to your computer and use it in GitHub Desktop.
Save kvdesa/e7390272e75eca2080590e4fc54ff42c to your computer and use it in GitHub Desktop.
Helpers to enable landscape, light and dark modes in preview.

Enable landscape in preview

Helper struct to enable landscape in preview.

Landscape {
    ContentView()
        .previewDevice("iPhone Xʀ")
        .landscape()
}

Eanble light and dark modes in preview

Helper struct to enable both light and dark modes in preview.

LightAndDark {
    ContentView()
        .previewDevice("iPhone Xʀ")
}
#if DEBUG
struct LandscapeModifier: ViewModifier {
let height = UIScreen.main.bounds.width
let width = UIScreen.main.bounds.height
func body(content: Content) -> some View {
content
.previewLayout(.fixed(width: width, height: height))
.environment(\.horizontalSizeClass, .compact)
.environment(\.verticalSizeClass, .compact)
}
}
extension View {
func landscape() -> some View {
modifier(LandscapeModifier())
}
}
struct LightAndDark<Content>: View where Content: View {
let content: () -> Content
var body: some View {
ForEach(ColorScheme.allCases, id: \.self) { colorScheme in
content()
.environment(\.colorScheme, colorScheme)
.previewDisplayName("\(colorScheme)")
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment