2

I have been looking around but couldn't find the solution so lets hope someone can help me.

I have a ScrollView with a LazyVStack in it. Then I have a list of 'cells'. Simplified it looks something like

Button(action: { ... }) {
    HStack {
        Text("Title")
        Button(action: { ... }) {
            Text("Sub action")
        }
        .buttonStyle(FancyButtonStyle())
    }
}
.buttonStyle(CellBackgroundStyle())

The correct actions are triggered when tapped. But the problem is that when I tap the 'sub action' the 'CellBackgroundStyle' is triggered.

I did consider making the main button as the background but that did not work. Also putting them below each other would not work since I want a 'full cell tap animation'.

So does anyone here can explain to me how we can nest buttons in SwiftUI where the child does not trigger the parent animation?

Thanks in advance =]

Saren Inden
  • 3,450
  • 4
  • 32
  • 45
  • I don't think nesting buttons is a great idea. Maybe I would try a button for the sub action and a tap gesture for the cell. – EmilioPelaez Jul 11 '22 at 07:36
  • @EmilioPelaez but by using a tap gesture I cannot simply update the background color right? Using buttons with a custom configuration gives you the 'power' to react to the pressed state. – Saren Inden Jul 11 '22 at 08:09
  • You cannot avoid that effect, because your variant is not a "nested" button, but your internal button is actually a body of external button, so internal is a external (like a nail driven into table). A "nested" in SwiftUI concept would be `Button().overlay(Button())` and then you manipulate them separately (like a cup on the table). – Asperi Jul 11 '22 at 08:31
  • @Asperi thanks for your comment. I now ended up using a ZStack and setting all the views (except for the buttons) to disable hit testing. Using it with the background modifier did not work. Will also try overlay now but I think that will most likely result in the same 'issue' as with ZStack that I need to disable the hit test for elements like Text and Image. – Saren Inden Jul 11 '22 at 11:00

0 Answers0