我正在尝试创建一个自定义 SwiftUI 视图,它的作用类似于默认视图,我可以在其中使用方法或可选的初始值设定项参数向视图添加额外的内容。
SomeCustomView(title: "string argument") {
// some view
}
SomeCustomView(title: "hello") {
// some view
}.sideContent {
// another view
}
// This style is acceptable too
SomeCustomView(title: "hello", sideContent: { /* another view */ }) {
// some view
}
Run Code Online (Sandbox Code Playgroud)
如何修改此视图结构以使其行为类似于上述示例?
struct SomeCustomView<Content>: View where Content: View {
let title: String
let content: Content
init(title: String, @ViewBuilder content: () -> Content) {
self.title = title
self.content = content()
}
var body: some View {
VStack {
Text(title) …Run Code Online (Sandbox Code Playgroud)