UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Performance issues when adding shadows to a bunch of views.

Forums > SwiftUI

I'm trying to add a shadow to my grid of 118 items. But when I do it, I get frame drops when scrolling. This is my code. How can I fix this? ​ ScrollView { LazyVGrid(columns: layout) { ForEach(0..<filteredElementArray.count, id: .self) { ElementView(element: filteredElementArray[$0]) .clipShape(RoundedRectangle(cornerRadius: 10)) .shadow(radius: 4) .frame(width: 80, height: 80, alignment: .center) } } .padding() } .frame(minWidth: 0, maxWidth: .infinity)

2      

When posting code, put three backticks ``` on the line before and three backticks ``` on the line after in order to format your code so it can be read easily.

ScrollView { LazyVGrid(columns: layout) { ForEach(0..<filteredElementArray.count, id: .self) { ElementView(element: filteredElementArray[$0]) .clipShape(RoundedRectangle(cornerRadius: 10)) .shadow(radius: 4) .frame(width: 80, height: 80, alignment: .center) } } .padding() } .frame(minWidth: 0, maxWidth: .infinity)

vs

ScrollView {
    LazyVGrid(columns: layout) {
        ForEach(0..<filteredElementArray.count, id: .self) {
            ElementView(element: filteredElementArray[$0])
                .clipShape(RoundedRectangle(cornerRadius: 10))
                .shadow(radius: 4)
                .frame(width: 80, height: 80, alignment: .center)
        }
    }
    .padding()
}
.frame(minWidth: 0, maxWidth: .infinity)

Yeah, you probably shouldn't be doing that; it's computationally very expensive.

You could try putting .drawingGroup() after the .shadow() modifier. That will pre-render a rasterized version of the shadow, but it might not look as good.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.