[英]How to pass view as Parameter in a function Swift UI
There are several posts on how to pass a view to a struct using:有几篇关于如何使用以下方法将视图传递给结构的帖子:
struct ContainerView<Content: View>: View {
let content: Content
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content()
}
var body: some View {
content
}
}
But how do you pass a view as a parameter in a function?但是如何在 function 中将视图作为参数传递?
You can actually pass a view as a generic:您实际上可以将视图作为泛型传递:
func functionName<T:View>(viewYouArePassing: T){}
You also can use AnyView ,您也可以使用 AnyView ,
struct Whatever {
var view: AnyView
}
let myWhatever = Whatever(view: AnyView(CustomView))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.