简体   繁体   中英

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 ↓. 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 )

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 . So I can pass the action to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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