Skip to content

Instantly share code, notes, and snippets.

@markiv
Created May 2, 2024 08:27
Show Gist options
  • Save markiv/350a5e96850811035f6c3c2b5a8eff87 to your computer and use it in GitHub Desktop.
Save markiv/350a5e96850811035f6c3c2b5a8eff87 to your computer and use it in GitHub Desktop.
Measures the size of any view
import SwiftUI
struct MeasureSizeModifier: ViewModifier {
@Binding var size: CGSize
func body(content: Content) -> some View {
content.background {
GeometryReader { geometry in
Color.clear
.onAppear { size = geometry.size }
.onChange(of: geometry.size) { size = geometry.size }
}
}
}
}
extension View {
func measure(size: Binding<CGSize>) -> some View {
modifier(MeasureSizeModifier(size: size))
}
}
@markiv
Copy link
Author

markiv commented May 2, 2024

struct ContentView: View {
    @State var size: CGSize = .zero

    var body: some View {
        VStack {
             
        }
        .measure(size: $size)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment