[英]How to pass view of different type to another view in SwiftUI?
So I have this TestView
which accepts headerContent
and bodyContent
,所以我有这个接受
headerContent
和bodyContent
的TestView
,
struct TestView<Content: View>: View {
var headerContent: (() -> Content)? = nil
let bodyContent: () -> Content
var body: some View {
VStack {
headerContent?()
bodyContent()
}
}
}
And I use it as,我用它作为,
struct ContentView: View {
var body: some View {
TestView(headerContent: {
Text("HeaderContent")
}) {
ScrollView {
}
}
}
}
But I get the following error,但我收到以下错误,
Cannot convert value of type 'ScrollView<EmptyView>' to closure result type 'Text'
What am I missing?我错过了什么?
You need to have two View
generics, since headerContent
and bodyContent
are not the same.你需要有两个
View
generics,因为headerContent
和bodyContent
不一样。
Without this, you are saying there is some concrete type Content
that conforms to View
.没有这个,你说的是有一些符合
View
的具体类型Content
。 However, both Text
and ScrollView
are different types, not the same.但是,
Text
和ScrollView
都是不同的类型,并不相同。
Code:代码:
struct TestView<HeaderContent: View, BodyContent: View>: View {
var headerContent: (() -> HeaderContent)? = nil
let bodyContent: () -> BodyContent
var body: some View {
VStack {
headerContent?()
bodyContent()
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.