【问题标题】:SwiftUI Custom View WrapperSwiftUI 自定义视图包装器
【发布时间】:2021-03-03 00:03:14
【问题描述】:

我正在试图弄清楚如何制作一个自定义视图来包装像这样的普通 SwiftUI 内容......我不确定我是否使用 UIViewRepresentable 或什么。请帮忙。

CustomView { x in 
   VStack { ... }
}

【问题讨论】:

    标签: swift swiftui uikit


    【解决方案1】:

    您可以使用此向下代码并将您的 SwiftUI 内容提供给 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. 
    
        }
    }
    

    用例:

    struct ContentView: View {
    
            var body: some View {
                
                CustomView(content: {
    
                    VStack { Text("Hello, world!").padding() }
                })
                
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      • 2020-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      相关资源
      最近更新 更多