Home > Enterprise >  How to add a button to end of List
How to add a button to end of List

Time:05-03

I have a list where I am trying to add a button at the end of it. I would like the button to appear in the list itself as opposed to displaying outside the list.

I only want the button to show up at the end of the list, and not repeat for each item. Any feedback is greatly appreciated.

struct Castaways{
    
        let id = UUID()
        let name: String
    
}

struct Test: View {
    @State private var castaways: [Castaways] = [
            Castaways(name: "Locke"),
            Castaways(name: "Jack"),
            Castaways(name: "Sawyer")
        ]
    
    var body: some View {
            List(castaways, id: \.id){ person in
                Text(person.name)
            }
        }
    }

struct Test_Previews: PreviewProvider {
    static var previews: some View {
        Test()
    }
}

CodePudding user response:

Use ForEach inside the List

var body: some View {
    List {
        ForEach(castaways, id: \.id){ person in
            Text(person.name)
        }
        Button("Tap me") { print("tapped") }
    }
}
  • Related