Post

Replies

Boosts

Views

Activity

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
2
0
324
Feb ’22
How to keep VStacks bounded by Menu Bar?
If I drop in a Color.gray into a Stack it fills all the space beneath the iOS menu bar (that shows the time and so on). But if I drop in a Spacer() and color the .background of the stack, it now fills the entire screen space and covers the menu bar. Why do color and images respect the menu bar but spacers do not? What is the most standard way to make a colored VStack that stays below the menu bar? I tried to use GeometryReader - .padding(.top, geometry.safeAreaInsets.top) - but to my surprise the safeAreaInset is more than the actual menu bar - this bounds the VStack to lower in height than just a Color.gray goes. If anyone can help me, I’d really appreciate it as I find this very mysterious and I don’t know the way forward. Thank you very much.
0
0
314
Jan ’22