Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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