skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    You can actually pass a view as a generic:

     func functionName<T:View>(viewYouArePassing: T){}
    

  2. You also can use AnyView,

    struct Whatever {
         var view: AnyView
    }
    
    let myWhatever = Whatever(view: AnyView(CustomView))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search