Skip to content

Instantly share code, notes, and snippets.

@sarunw
Created March 16, 2021 03:43
Show Gist options
  • Save sarunw/cfe5d0f2320fc311e073e72ffc41379e to your computer and use it in GitHub Desktop.
Save sarunw/cfe5d0f2320fc311e073e72ffc41379e to your computer and use it in GitHub Desktop.
Preview a device in landscape with SwiftUI Previews
struct LandscapeModifier: ViewModifier {
let height = UIScreen.main.bounds.width
let width = UIScreen.main.bounds.height
var isPad: Bool {
return height >= 768
}
var isRegularWidth: Bool {
return height >= 414
}
func body(content: Content) -> some View {
content
.previewLayout(.fixed(width: width, height: height))
.environment(\.horizontalSizeClass, isRegularWidth ? .regular: .compact)
.environment(\.verticalSizeClass, isPad ? .regular: .compact)
}
}
extension View {
func landscape() -> some View {
self.modifier(LandscapeModifier())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment