swiftuiios15ipados15

SwiftUI on iOS 15, custom buttonStyle doesn't work anymore, it works fine on iOS 14, how to fix?


As the title described. I was using a custom SwiftUI buttonStyle on iOS 14. It works fine, but now it's not works on iOS 15. There is no error no warning, I don't know how to deal it. Anybody know to fix it?

struct InnerShadowButtonStyle: ButtonStyle {

    func makeBody(configuration: Configuration) -> some View {
        
        let p = configuration.isPressed
        
        return configuration.label
        
          .overlay (
            RoundedRectangle(cornerRadius: 0)
                .stroke(Color.clear, lineWidth: 4)
                .shadow(color: p ? .gray : .clear, radius: 3, x: 3, y: 3)
                .clipShape(RoundedRectangle(cornerRadius: 0))
                .opacity(p ? 1.0 : 0.0)
          )
    }
}

Solution

  • Seems like SwiftUI 3 doesn't draw transparent rectangle at all, as you stroke it with Color.clear.

    You can force it with something like Color.black.opacity(0.001):

    configuration.label
        .overlay (
            RoundedRectangle(cornerRadius: 0)
                .stroke(Color.black.opacity(0.001), lineWidth: 4)
                .shadow(color: p ? .gray : .clear, radius: 3, x: 3, y: 3)
                .clipShape(RoundedRectangle(cornerRadius: 0))
                .opacity(p ? 1.0 : 0.0)
        )