Created
January 17, 2022 21:11
-
-
Save emma-k-alexandra/238b87c0b990c740e97930fe2061ae12 to your computer and use it in GitHub Desktop.
Assign the size of a SwiftUI View to a Binding - Based on Federico Zanetello's `readSize`
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
import SwiftUI | |
/* | |
Example | |
struct MyView: View { | |
@State private var childSize = CGSize.zero | |
var body: some View { | |
ChildView() | |
.assignSize(to: $childSize) | |
} | |
} | |
*/ | |
extension View { | |
func assignSize(to binding: Binding<CGSize>) -> some View { | |
readSize { size in | |
binding.wrappedValue = size | |
} | |
} | |
} | |
// From Federico's original work | |
// Gist: https://github.com/FiveStarsBlog/CodeSamples/blob/main/SwiftUI-read-a-view-size/View+readSize.swift | |
// Explainer blog post: https://fivestars.blog/articles/swiftui-share-layout-information/ | |
struct SizePreferenceKey: PreferenceKey { | |
static var defaultValue = CGSize.zero | |
static func reduce(value: inout CGSize, nextValue: () -> CGSize) {} | |
} | |
extension View { | |
func readSize(onChange: @escaping (CGSize) -> Void) -> some View { | |
background( | |
GeometryReader { geometryProxy in | |
Color.clear | |
.preference(key: SizePreferenceKey.self, value: geometryProxy.size) | |
} | |
) | |
.onPreferenceChange(SizePreferenceKey.self, perform: onChange) | |
} | |
} | |
/* | |
Example: | |
var body: some View { | |
childView | |
.readSize { newSize in | |
print("The new child size is: \(newSize)") | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment