如何使用 SwiftUI 在另一个视图中创建视图?

Nic*_*lov 6 ios swift swiftui

我需要做这么简单的事情,但不知道怎么做。

所以我需要创建一个视图,我已经在另一个视图中。这里现在怎么样了?

这是我的按钮

struct CircleButton: View {
    var body: some View {

        Button(action: {
            self

        }, label: {
            Text("+")
                .font(.system(size: 42))
                .frame(width: 57, height: 50)
                .foregroundColor(Color.white)
                .padding(.bottom, 7)
        })
        .background(Color(#colorLiteral(red: 0.4509803922, green: 0.8, blue: 0.5490196078, alpha: 1)))
        .cornerRadius(50)
        .padding()
        .shadow(color: Color.black.opacity(0.15),
                radius: 3,
                x: 0,
                y: 4)
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我点击按钮时想要放置的视图?


struct IssueCardView: View {

    var body: some View {

        ZStack (alignment: .leading) {
            Rectangle()

                .fill(Color.white)
                .frame(height: 50)
                .shadow(color: .black, radius: 20, x: 0, y: 4)
                .cornerRadius(8)

            VStack (alignment: .leading) {

                    Rectangle()
                        .fill(Color(#colorLiteral(red: 0.6550863981, green: 0.8339114785, blue: 0.7129291892, alpha: 1)))
                        .frame(width: 30, height: 8)
                        .cornerRadius(8)
                        .padding(.horizontal, 10)


                    Text("Some text on card here")
                        .foregroundColor(Color(UIColor.dark.main))
                        .font(.system(size: 14))
                        .fontWeight(.regular)
                        .padding(.horizontal, 10)
            }

        }
    }
}

Run Code Online (Sandbox Code Playgroud)

这是我想要放置此 IssueCardView 的视图?。而不是像现在这样手动执行,我想用按钮生成这个视图。

struct TaskListView: View {

    var body: some View {
        ScrollView(.vertical, showsIndicators: false, content: {
            VStack (alignment: .leading, spacing: 8) {

                **IssueCardView()
                IssueCardView()
                IssueCardView()
                IssueCardView()
                IssueCardView()**

            }
            .frame(minWidth: 320, maxWidth: 500, minHeight: 500, maxHeight: .infinity, alignment: .topLeading)
            .padding(.horizontal, 0)



        })
    }
}
Run Code Online (Sandbox Code Playgroud)

Moj*_*ini 2

尽管它可以与scrollView和 一起使用stack,但您应该使用List来解决此类 UI 问题(正如您在 任务列表视图的名称中已经提到的那样)

struct TaskListView: View {

    typealias Issue = String // This is temporary, because I didn't have the original `Issue` type

    @State var issues: [Issue] = ["Some issue", "Some issue"]

    var body: some View {
        ZStack {
            List(issues, id: \.self) { issue in
                IssueCardView()
            }

            CircleButton {
                self.issues += ["new issue"]
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经添加let action: ()->()CircleButton. 这样我就可以将操作传递给它。