0

Hello I am trying to simply add a swipe action to a list of subviews In the parent view. Each subview is its own navigation link and putting the swipe action is not working. How can I fix this. I don't get any errors though in the code.

struct Tip: Identifiable, Codable, Hashable{
    var id: String
    var caption: String
    var link: String
}
                    ForEach(viewModel.ThreeTips) { tip in
                        TipRowView(tip: tip)
                            .swipeActions {
                                Button {
                                    
                                } label: {
                                    Text("delete")
                                }
                            }
                            .tint(.red)
                    }

1 Answers1

1

I believe swipeActions is meant for List (and it works there):

List(viewModel.ThreeTips, id: \.self) { tip in
  TipRowView(tip: tip)
    .swipeActions {
      Button {                                    
      } label: {
        Text("delete")
      }
    }
    .tint(.red)                
}
lazarevzubov
  • 1,767
  • 2
  • 14
  • 24
  • thanks, that works. Is there no way to use the forEach. When I use the list it puts the elements in a weird view and adds padding to each side –  Jan 31 '23 at 04:40
  • `List` has the `listStyle(_:)` modifier: https://developer.apple.com/documentation/swiftui/view/liststyle(_:) Applying one of the pre-defined styles should resolve the issue. Perhaps, `PlainListStyle` will work: https://developer.apple.com/documentation/swiftui/plainliststyle. Here's the discussion on SO: https://stackoverflow.com/questions/56614080/how-to-remove-the-left-and-right-padding-of-a-list-in-swiftui. – lazarevzubov Jan 31 '23 at 07:55