Timmerz
Timmerz

Reputation: 6199

SwiftUI Custom View Wrapper

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 { ... }
}

Upvotes: 5

Views: 5605

Answers (1)

swiftPunk
swiftPunk

Reputation: 1

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() }
            })
            
   }
}

Upvotes: 17

Related Questions