Home > Blockchain >  SwiftUI Button cannot be clicked on its whole area
SwiftUI Button cannot be clicked on its whole area

Time:12-28

I built a custom Button.
My problem is, that it can be only clicked on the Text Elements not on the whole area of the Button.
What's going wrong, how can I solve this?

struct MyButton: View {
  
  var body: some View
  { Button( action:{ print("pressed") })
  { HStack {
      VStack(alignment: .leading){
        Text("Tomorrow").font(.caption)
        Text("23.5.22 KW 23")
      }
      Spacer()
    }
  }
  .padding([.horizontal],4.0)
  .padding([.vertical],2.0)
  .background(RoundedRectangle(cornerRadius: 5).fill(Color.red))
  .buttonStyle(PlainButtonStyle())
  .padding([.horizontal],8.0)
  }
}

CodePudding user response:

By default, only the parts of the view that actually render something are tappable. You need to add a .contentShape(Rectangle()) modifier to your Button to make the entire area interactive.

  • Related