Search code examples
buttonswiftuitvostvos13

Adding shadow on view breaks focusable buttons within it


When I add shadow on a view that contains a button then the button will no longer be clickable. The same code on iOS works as expected, but on tvOS it breaks the button. Am I doing something wrong here? Is there a workaround?

Here is my code,

VStack {
  Button(action: {
    print("Button clicked") // This is never called
  }) {
    Text("test")
  }
  .padding()
}
.background(Color.red)
.shadow(color: Color.black, radius: 14, x: 0, y: 4)

Solution

  • I found a workaround for this issue, since backgrounds can be set to any view you can apply all the styling you need to the background, and it won't break the buttons within the view.

    .background(RoundedRectangle(cornerRadius: 8)
      .fill(Color.red)
      .shadow(color: Color.black, radius: 14, x: 0, y: 4))