【问题标题】:How can I use a VStack as input for a func in SwiftUI?如何在 SwiftUI 中使用 VStack 作为 func 的输入?
【发布时间】:2021-01-22 23:59:30
【问题描述】:

我刚刚创建了一个函数,将 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))
    
}

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    添加视图约束

    func appendThisVStack<Content: View>(vStack: Content) { //<-- Here
        
        myAnyVStacks.append(AnyView(vStack))
        
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-24
      • 1970-01-01
      • 2021-02-18
      • 1970-01-01
      • 1970-01-01
      • 2020-04-24
      • 2020-07-01
      • 2021-11-07
      • 2021-09-04
      相关资源
      最近更新 更多