Recipe for measuring a view in SwiftUI:
1. Add a background or overlay to your view.
2. Add a GeometryReader to that background or overlay.
3. Add an empty view, for example a clear color, to that GeometryReader.
4. Listen for changes to the geometry passed to you by the GeometryReader.
Sure, we may understand the code but how did we end up here? Surely this isn’t intuitive to anyone.
(Sorry for being old and grumpy)
@simonbs Have you looked at anchorPreferences? I’ve used this in a few cases to gather size data about child views for use in a parent without needing a GeometryReader (hopefully not misunderstanding the issue 😅)
https://swiftui-lab.com/communicating-with-the-view-tree-part-2/
@simonbs replying in case its useful:
.onGeometryChange { geo in }
https://gist.github.com/shaps80/44f82efd1ba0dad2ee8235fdff80f1d8
The other replies essentially cover this but thought I’d share my modifier since I’ve not seen it elsewhere.