I'd like to calculate the line width of a shape inside a view based on the view's size. Looking through various posts here on StackOverflow, I think the solution is to use a GeometryReader like this:
struct MyView: View {
var body: some View {
GeometryReader { geometry in
// Here goes your view content,
// and you can use the geometry variable
// which contains geometry.size of the parent
// You also have function to get the bounds
// of the parent: geometry.frame(in: .global)
}
}
}
My question is, how can I define variables inside the GeometryReader construct to be used for the view? I've tried to put a var statement directly after the line "GeometryReader { geometry in", but this gives a compiler error.
MyView
, or its parent? IsMyView
the "line width of a shape" (whereGeometryReader
would be the dimensions of the parent view) or is this "shape" a childView
insideMyView
?SwiftUI
is aView
- it sounds like you are trying to work with aCAShapeLayer
frame based on theUIView
dimensions.