6

I need to make such a simple thing, but can't figure out how.

So I need to create a View which I already have 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)
        })
    }
}

1 Answer 1

2

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

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. That works. Another question that I have button in separate view cause in future it would have an ability to add this IssueCardView in different TaskListViews. May be you can help me to figure out how this button could make this action more autonomous?
Feel free to ask a new question as you need. Also you can comment below this post a link to the new question, so I notify and help you if I can ;)
Sorry, where have you added the "let action: ()->()" exactly?
Exactly at the first line of the custom button view
About your other question, you should make a model for your application. Then you can publish it changes to all subscribers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.