I'm trying to pass a custom View struct with a @Namespace property for a .matchedGeometryEffect to a parent View.
Since the parent will be providing the Namespace I am using a custom init.
When I use the syntax analogous to the @Binding custom initialization Xcode forces me to use the wrapper when initializing my custom View. That in turn kills my .matchedGeometryEffect.
struct MyView<Content: View>: View {
@Binding var matched: Bool
@Namespace var nspace
let content: Content
init(matched: Binding<Bool>,
nspace: Namespace,
@ViewBuilder content: @escaping () -> Content
) {
self._matched = matched
self._nspace = nspace
self.content = content()
}
var body: some View {
...
}
}
What seems to work is using var nspace: Namespace.ID instead of @Namespace var nspace and then:
struct MyView<Content: View>: View {
@Binding var matched: Bool
var nspace: Namespace.ID
let content: Content
init(matched: Binding<Bool>,
nspace: Namespace.ID,
@ViewBuilder content: @escaping () -> Content
) {
self._matched = matched
self.nspace = nspace
self.content = content()
}
var body: some View {
...
}
}
Can this cause trouble somewhere else? Is there a better way?