I implemented a BasicContainer upon GeometryReader
with the functionality of a @ViewBuilder
. How can I use this inside GeometryProxy
, outside when declaring the content?
Here is my BasicContainer:
struct BasicContainer<Content : View> : View {
let content : Content
init(@ViewBuilder content: () -> Content) {
self.content = content()
}
var body: some View {
GeometryReader { proxy in
content
.frame(width: proxy.size.width * 0.3, height: 200)
// do some other fancy stuff ...
}
}
}
Everything works fine, when I use the BasicContainer
as it's meant to be:
struct ContentView: View {
var body: some View {
BasicContainer {
Text("Roberts BasicContainer")
}
}
}
But what, if I would like to have the GeometryProxy also outside, (like Apple implemented their GeometryReader
), like so:
BasicContainer { outsideGeo in
Text("Width: \(outsideGeo.size.width)")
}
I guess some kind of a @Binding
could make the trick, but so far I couldn't make it work.
Is this even possible to realize?
Any help is very appreciated !!!
CodePudding user response:
This should work:
let content : (GeometryProxy) -> Content
init(@ViewBuilder content: @escaping (GeometryProxy) -> Content) {
self.content = content
}
then:
GeometryReader { proxy in
content(proxy)
.frame(width: proxy.size.width * 0.3, height: 200)
// do some other fancy stuff ...
}
edit: I assumed your geometryproxy is named proxy. -> Explicit implementation to avoid confusion.
when you call your container you can do:
BasicContainer { outsideGeo in
Text("Width: \(outsideGeo.size.width)")
}