我刚刚制作了一个函数,将Text作为输入并将该Text添加到AnyView数组中,我想使用VStack做同样的事情,这里我的Text示例代码正在运行,只是想在VStack中使用相同的功能,感谢阅读和您的时间。
struct ContentView: View {
init() {
appendThisText(text: Text("Hello, world1!") )
appendThisText(text: Text("Hello, world2!") )
appendThisText(text: Text("Hello, world3!") )
}
var body: some View {
ForEach(myAnyTexts.indices, id:\.self) { index in
myAnyTexts[index]
}
}
}
var myAnyTexts: [AnyView] = [AnyView]()
func appendThisText(text: Text) {
myAnyTexts.append(AnyView(text))
}
var myAnyVStacks: [AnyView] = [AnyView]()
func appendThisVStack(vStack: VStack<Content: View>) { // Here: the problem!
myAnyVStacks.append(AnyView(vStack))
}
为您更新了质量代码:
var myAnyContents: [AnyView] = [AnyView]()
func appendThisContent<Content: View>(content: Content) {
myAnyContents.append(AnyView(content))
}
添加视图约束
func appendThisVStack<Content: View>(vStack: Content) { //<-- Here
myAnyVStacks.append(AnyView(vStack))
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句