NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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)

   

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.

   

Hacking with Swift is sponsored by Instabug

SPONSORED Catch bugs as soon as they happen and know exactly why a crash occurred. Instabug's SDK grabs all the logs they need to fix bugs, crashes and performance issues in minutes instead of days. Get screenshots, device details, network logs, repro steps, and tons of other critical insights needed to resolve issues and prioritize product backlogs straight from your dashboard. It only takes a minute to integrate!

Get started now

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

Reply to this topic…

You need to create an account or log in to reply.

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.