我很好奇您是否可以在 SwiftUI 中创建自定义容器 View 。我知道您可以创建自定义内容 View ,例如 Text()
,但不确定您是否可以复制 HStack { }
类似于:
HGrid {
Text("Lorem ipsum")
Text("Lorem ipsum")
}
然后自定义容器 View (又名 HGrid
)会在每个添加的组件之间添加 Spacer()
。本质上,例如,将其转译为:
HStack {
Text("Lorem ipsum")
Spacer()
Text("Lorem ipsum")
}
最佳答案
SwiftUI
使用 ViewBuilder
@functionBuilder
struct HGrid <Content: View>: View {
init(@ViewBuilder builder: () -> Content) {
let content = builder()
...
}
}
另见
关于swift - 如何在 SwiftUI 中添加自定义容器 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56531763/