How to use UIBlurEffect for Buttons in Swift

39 views Asked by At

I'm trying to add .systemThinMaterial to my SwiftUI buttons but after 8 buttons it starts lagging when I scroll. Is it too intensive to use that for button? or em I doing it wrong?

struct BlurryButton: UIViewRepresentable {
    var style: UIBlurEffect.Style
    
    func makeUIView(context: UIViewRepresentableContext<Self>) -> UIVisualEffectView {
        UIVisualEffectView(effect: UIBlurEffect(style: style))
    }
    
    func updateUIView(_ uiView: UIVisualEffectView, context: UIViewRepresentableContext<Self>) {
        uiView.effect = UIBlurEffect(style: style)
    }
}

Then for my Button style I use

struct DefaultStyle: ButtonStyle {
    @Environment(\.colorScheme) var colorScheme

    func makeBody(configuration: Configuration) -> some View {
        configuration.label

            .background(BlurryButton(style: .systemThinMaterial))

            .foregroundColor(colorScheme == .dark ? Color.white : Color.black)
            .opacity(configuration.isPressed ? 0.7 : 1.0)
          }
       }
0

There are 0 answers