Created
March 9, 2020 13:31
-
-
Save chriseidhof/7a861afa58d098be64586025020bd65f to your computer and use it in GitHub Desktop.
SwiftUI helpers
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
struct <#MyView#>: View { | |
var body: some View { | |
<#body#> | |
} | |
} | |
extension View { | |
func geometryPreference<P: PreferenceKey>(_ key: P.Type, value: @escaping (GeometryProxy) -> P.Value) -> some View { | |
overlay(GeometryReader { proxy in | |
Color.clear.preference(key: P.self, value: value(proxy)) | |
}) | |
} | |
} | |
struct Preference<P: PreferenceKey, Content: View>: View where P.Value: Equatable { | |
let content: (P.Value) -> Content | |
@State private var value: P.Value = P.defaultValue | |
init(_ key: P.Type, @ViewBuilder content: @escaping (P.Value) -> Content) { | |
self.content = content | |
} | |
var body: some View { | |
content(value) | |
.onPreferenceChange(P.self) { self.value = $0 } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment