简体   繁体   English

如何在 function Swift UI 中将视图作为参数传递

[英]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.

相关问题 如何在swift中将touchUpInside函数作为参数传递? - How to pass touchUpInside function as parameter in swift? 如何传递一个数组作为 Swift 中的参数? - How to pass an Array to function as a parameter in Swift? 如何将NSNotification作为参数传递给Swift中的选择器函数 - how to pass an NSNotification as parameter to the selector function in Swift 如何创建一个指针/传递参数以在Swift中运行 - How to create a pointer/pass parameter to function in Swift 如何将 SwiftUI View 作为函数参数传递? - How to pass SwiftUI View as function parameter? 如何将UI ImageView从一个视图控制器传递到另一个? 迅捷3 - how to pass a UI ImageView from one view controller to another? swift 3 Swift:如何有选择地将函数作为参数传递并调用函数? - Swift: how to optionally pass function as parameter and invoke the function? 如何在 swift 4 中的 function 中动态地将结构的属性 select 作为参数传递? - How to select a property of a struct dynamically to pass as a parameter in a function in swift 4? 如何使用 swift 将 UIVIewController 名称作为参数传递给特定函数? - How to pass UIVIewController name as a parameter to particular function using swift? 如何在Swift中传递参数未知且返回值作为参数的函数 - How to pass a function with unknown parameters and returned value as a parameter in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM