Ios 如何在SwiftUI中将视图设置为参数

Ios 如何在SwiftUI中将视图设置为参数,ios,swift,swiftui,Ios,Swift,Swiftui,我试图通过配置结构提供一个选项来显示自定义视图。我不知道如何做到这一点。我尝试了许多选项,但不断出现编译错误。可能是我这样做的方法不对!我感谢你的帮助 以下是我尝试过的: 配置结构,提供设置颜色、宽度和自定义内容等的选项 struct Configuration { var color: Color .... var customView: (([String])->AnyView?) = nil // <- this compiles but not sur

我试图通过配置结构提供一个选项来显示自定义视图。我不知道如何做到这一点。我尝试了许多选项,但不断出现编译错误。可能是我这样做的方法不对!我感谢你的帮助

以下是我尝试过的:

配置结构,提供设置颜色、宽度和自定义内容等的选项

struct Configuration {
    var color: Color
    ....
    var customView: (([String])->AnyView?) = nil // <- this compiles but not sure if this is the right way to do it!
    ...
}
下面是我如何使用它以及从哪里得到编译器错误:

struct ContentView: View {
    var config = Configuration()

    config.customView = CustomView(["A", "B"]) // <-- Error: Cannot assign value of type CustomView to type ([String]) -> AnyView

    var body: some View {
        VStack {
            .... // display other content
        }
        .overlay(showCustomView())
    }

    @ViewBuilder
    private func showCustomView() -> some View {
        if let customContent = config.customView {
            customContent()
        } else {
            EmptyView()
        }

    }
}
struct ContentView:View{
var config=Configuration()
config.customView=customView([“A”,“B”])//AnyView
var body:一些观点{
VStack{
..//显示其他内容
}
.overlay(showCustomView())
}
@视图生成器
private func showCustomView()->某些视图{
如果让customContent=config.customView{
自定义内容()
}否则{
EmptyView()
}
}
}
这里是一个可能的解决方案(据我所知,您的意图是)-您需要配置中的泛型,以便能够在任何视图中使用它

这是一个演示。用Xcode 12.4编写

struct Configuration<V: View> {   // << view dependent
    var color: Color
    var customView: (([String]) -> V?)? = nil    // << view builder
}

struct CustomView: View {   // no changes
    var values: [String]
    var body: some View {
        VStack {
            ForEach(values.indices, id:\.self) { index in
                Text(values[index])
            }
        }
    }
}

struct ContentView: View {

    var config = Configuration(color: .black) {   // << create config !!
        CustomView(values: $0)                    // ... and specify view !!
    }

    var body: some View {
        VStack {
            Text("")
        }
        .overlay(showCustomView())
    }

    @ViewBuilder
    private func showCustomView() -> some View {
        if let customContent = config.customView {
            customContent(["A", "B"])              // << inject params !!
        } else {
            EmptyView()
        }
    }
}

struct Configuration{//V?=nil//这真的很好!一个问题,我必须使配置结构视图依赖吗?
struct Configuration<V: View> {   // << view dependent
    var color: Color
    var customView: (([String]) -> V?)? = nil    // << view builder
}

struct CustomView: View {   // no changes
    var values: [String]
    var body: some View {
        VStack {
            ForEach(values.indices, id:\.self) { index in
                Text(values[index])
            }
        }
    }
}

struct ContentView: View {

    var config = Configuration(color: .black) {   // << create config !!
        CustomView(values: $0)                    // ... and specify view !!
    }

    var body: some View {
        VStack {
            Text("")
        }
        .overlay(showCustomView())
    }

    @ViewBuilder
    private func showCustomView() -> some View {
        if let customContent = config.customView {
            customContent(["A", "B"])              // << inject params !!
        } else {
            EmptyView()
        }
    }
}