How can I refactor a GeometryReader?

This question has stumped me for some time, I'd really appreciate someone resolving this one. Thanks.

I depend on the GeometryReader for proportional resizing but I find myself writing boilerplate code that I would like to get rid of, like this:

struct myview : View {

var body: some View {

GeometryReader { g in

let l = g.size.length let w = g.size.width let x = l * 0.5 let y = w * 0.5

someView() .frame(width: (l * 0.25), height: (w * 0.7)) .position(x: x, y: y)

}

}

}

The point is that after those declarations, I can auto-center the views enclosed by the GeometryReader (by default, GeometryReader places them in the top left corner), and resize them effortlessly with the frame modifier by a scalar.

I want to avoid writing the same code over and over.

But the child view needs variables it can access to resize according to them.

I think I need to extend the GeometryReader class, but I don't know how.

The syntax ought to look like this:

customGeometryReader { l, w, x, y in

someView .frame(width: l, height: w) .position(x: x, y: y)

}

How do I extend the GeometryReader in this way?

Thanks very much

Value types in swift cannot be subclassed because they are not objects nor are they class types. You can try extending with the use of extensions but the internal behaviours of the components are private APIs except for what is already available as public APIs.

Thank you. What do you mean by value types cannot be subclassed? You mean structs? I believe the answer is @ObservableObject anyway. Thanks

How can I refactor a GeometryReader?
 
 
Q