Created
July 2, 2022 22:46
-
-
Save IanKeen/0bad608f92bd7e97244fbbc2f2e967fd to your computer and use it in GitHub Desktop.
PropertyWrapper: SwiftUI SizeClass helper
This file contains 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
//v1: just find out if sizeclass is regular | |
@propertyWrapper | |
struct SizeClass: DynamicProperty { | |
@Environment(\.horizontalSizeClass) var horizontalSizeClass | |
@Environment(\.verticalSizeClass) var verticalSizeClass | |
var wrappedValue: Bool { | |
horizontalSizeClass == .regular && verticalSizeClass == .regular | |
} | |
} | |
//v2: output specific values based on size class | |
@propertyWrapper | |
struct SizeClass<T>: DynamicProperty { | |
@Environment(\.horizontalSizeClass) var horizontalSizeClass | |
@Environment(\.verticalSizeClass) var verticalSizeClass | |
var regular: T | |
var compact: T | |
var wrappedValue: T { | |
return horizontalSizeClass == .regular && verticalSizeClass == .regular | |
? regular : compact | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment