1

I'm trying to add a new view inside the Scroll View that contains a button every time that I click in the blue button in the bottom

Text](https://stackoverflow.com/image.jpg) [![enter image description here]1

Here i create the scroll view with 2 buttons, and want to add more after I click in the button on the right

        HStack{
            ScrollView(.horizontal, content: {
                HStack{
                    PageStep()
                    PageStep()

                }
            })

            Button(action: {
                self.addNewStep = true
            }) {
                Image(systemName: "plus.square")
                    .frame(width: 75, height: 75)
                    .overlay(
                        RoundedRectangle(cornerRadius: 10)
                            .stroke(Color.blue, lineWidth: 5)
                )
            }.buttonStyle(PlainButtonStyle()).padding(.trailing, 10)
        }
        .padding(.leading, 10)
        .frame(minWidth: 0, maxWidth: UIScreen.main.bounds.width, minHeight: 80, alignment: .bottom)
        .foregroundColor(Color.blue)


struct PageStep: View {
    var stepPossition = String()

    var body: some View {

        Button(action: {
            print("Entrou")
        }){
            Rectangle()
                .fill(Color.red)
                .frame(width: 75, height: 75)
                .cornerRadius(10)
        }

    }
}

1 Answer 1

1

Here is possible approach. Tested with Xcode 11.4.

struct ContentView: View {
    @State private var steps = 2 // pages counter

    var body: some View {
        HStack{
            ScrollView(.horizontal, content: {
                HStack{
                    // create available pages
                    ForEach(0..<steps, id: \.self) { i in
                        PageStep(stepPossition: "\(i)").id(i) // inject
                    }
                }
            })

            Button(action: {
                self.steps += 1 // << add next page
            }) {
                Image(systemName: "plus.square")
                    .frame(width: 75, height: 75)
                    .overlay(
                        RoundedRectangle(cornerRadius: 10)
                            .stroke(Color.blue, lineWidth: 5)
                )
            }.buttonStyle(PlainButtonStyle()).padding(.trailing, 10)
        }
        .padding(.leading, 10)
        .frame(minWidth: 0, maxWidth: UIScreen.main.bounds.width, minHeight: 80, alignment: .bottom)
        .foregroundColor(Color.blue)

    }
}

struct PageStep: View {
    var stepPossition: String

    var body: some View {

        Button(action: {
            print("Entrou")
        }){
            Rectangle()
                .fill(Color.red)
                .frame(width: 75, height: 75)
                .cornerRadius(10)
        }

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.