I'm trying to create a simple struct
that accepts an array of Views and returns an ordinary VStack
containing those Views except they all are stacked diagonally.
Code:
struct LeaningTower<Content: View>: View {
var views: [Content]
var body: some View {
VStack {
ForEach(0..<views.count) { index in
self.views[index]
.offset(x: CGFloat(index * 30))
}
}
}
}
Now this works great but I always get annoyed whenever I have to call it:
LeaningTower(views: [Text("Something 1"), Text("Something 2"), Text("Something 3")])
Listing the views in an array like that seems extremely odd to me so I was wondering if there was a way I could use @ViewBuilder
to call my LeaningTower
struct like this:
LeaningTower { // Same way how you would create a regular VStack
Text("Something 1")
Text("Something 2")
Text("Something 3")
// And then have all of these Text's in my 'views' array
}
If there's a way to use @ViewBuilder
to create/extract an array of Views please let me know.
(Even if it isn't possible using @ViewBuilder
literally anything that will make it look neater will help out a lot)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…