简体   繁体   English

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

[英]How to create View inside another view with SwiftUI?

I need to make such a simple thing but can't figure out how.我需要做这么简单的事情,但不知道怎么做。

So I need to create a View wich I already has inside another view.所以我需要创建一个我已经在另一个视图中的视图。 Here how it looks like now ↓现在的样子↓

Here's my button这是我的按钮

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

Here's a view which I want to place when I tap the button ↓这是我点击按钮时想要放置的视图↓


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

        }
    }
}

Here's a view where I want to place this IssueCardView ↓.这是我要放置此 IssueCardView ↓ 的视图。 Instead of doing it manually like now I want to generate this View with button.而不是像现在这样手动操作,我想用按钮生成这个视图。

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)



        })
    }
}

Although it works with scrollView and stack , You should use a List for these kind of UI issues (as you already mentioned in the name of Task List View )虽然它适用于scrollViewstack ,但您应该使用List来解决这些类型的 UI 问题(正如您在Task List View的名称中已经提到的)

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"]
            }
        }
    }
}

I have added let action: ()->() to CircleButton .我已将let action: ()->()添加到CircleButton So I can pass the action to it.所以我可以将动作传递给它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM