Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
// Original article here:
import SwiftUI
extension View {
func readSize(onChange: @escaping (CGSize) -> Void) -> some View {
GeometryReader { geometryProxy in
.preference(key: SizePreferenceKey.self, value: geometryProxy.size)
.onPreferenceChange(SizePreferenceKey.self, perform: onChange)
private struct SizePreferenceKey: PreferenceKey {
static var defaultValue: CGSize = .zero
static func reduce(value: inout CGSize, nextValue: () -> CGSize) {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment