Created
March 16, 2021 03:43
-
-
Save sarunw/cfe5d0f2320fc311e073e72ffc41379e to your computer and use it in GitHub Desktop.
Preview a device in landscape with SwiftUI Previews
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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