I've been playing with this MirrorView to display SwiftUI Hierarchy in a Tree. It's works pretty great until a custom View is introduced. Then the body type of the View is not shown, only its name.
struct ContentView: View {
var body: some View {
VStack {
Text("Lorem Ipsum")
CustomView()
}
.mirror()
}
}
struct CustomView: View {
var body: some View {
HStack {
Text("Hello")
Text("Word")
}
}
}
The problem is that the Mirror
for ContentView
is VStack<TupleView<(Text, CustomView)>>.
I was actually able to solve the problem by propagating the CustomView.Body type through PreferenceKey and replace the occurrence of ContentView by CustomView<HStack<TupleView<(Text, Text)>>>.
The downside of this approach is that I have to manually propagate the body type for every custom view.
struct CustomView: View {
var body: some View {
HStack {
Text("Hello")
Text("Word")
}
.propagateBodyType(self)
}
}
I wonder if there is a way to go from VStack<TupleView<(Text, CustomView)>> to VStack<TupleView<(Text, CustomView<HStack<TupleView<(Text, Text)>>>)>> just using reflection. Does anyone have any ideas?