Skip to content

Instantly share code, notes, and snippets.

@IanKeen
Created July 2, 2022 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IanKeen/0bad608f92bd7e97244fbbc2f2e967fd to your computer and use it in GitHub Desktop.
Save IanKeen/0bad608f92bd7e97244fbbc2f2e967fd to your computer and use it in GitHub Desktop.
PropertyWrapper: SwiftUI SizeClass helper
//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