5

I'm trying to figure out how to make a custom view which wraps normal SwiftUI content like this...I'm not sure if I use UIViewRepresentable or what. Please help.

CustomView { x in 
   VStack { ... }
}
0

1 Answer 1

17

you can use this down code and give your SwiftUI contents to content of CustomView:

struct CustomView <Content: View>: View {
    
    var content: () -> Content
    
    init(@ViewBuilder content: @escaping () -> Content) { self.content = content }
    
    var body: some View {

            content()  // <<: Do anything you want with your imported View here. 

    }
}

The use case:

struct ContentView: View {

        var body: some View {
            
            CustomView(content: {

                VStack { Text("Hello, world!").padding() }
            })
            
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.