SwiftUI App crashes while switching orientation

Hi,

I am a new SwiftUI app developer and developing my first application. In the process of designing not very GUI rich app, I noticed my app crashed whenever I switched orientation (testing on multiple iPhone devices).

After going through all kind of logs and errors, performance enhancements nothing worked.

Then I started rolling back all GUI related features 1 by 1 and tested (I am sure there are better approaches, but excuse me I am novice in app development :) ). Even though it's time consuming, I could pin point the cause of the fatal error and app crash, it's due to multiple .shadow modifiers I used on NavigationLink inside a ForEach look (to be precise I used it like following, .shadow(radius: 15) .shadow(radius: 20) .shadow(radius: 20) .shadow(radius: 20) .shadow(radius: 20)

Note, there are only 7 items in List and it uses the Hero View (like app store's Today section) for child views.

Once I got rid of shadow modifies or used only 1 app works fine and doesn't crash.

Lesson learnt...

P.S. It's so ironic that so performance tuned devices couldn't handle this basic GUI stuff.

Welcome to the forum.

Could you post minimal code so that we can try to reproduce and try to understand ?

Maybe that's just an error in your code…

Here is the Home view...

struct HomeView: View {

@Namespace var namespace var homeWidgetConfigurations = LoadHomeWidgetConfigurations() @State private var scale: CGFloat = 1.0 @State private var offset: CGFloat = 0 @Environment(.colorScheme) private var deviceColorScheme

var body: some View {
    GeometryReader { geometry in
        VStack {
            NavigationStack {
                ScrollView {
                    Label("Home", systemImage: "house.fill")
                        .font(.title2)
                        .fontWeight(.bold)
                        .foregroundColor(deviceColorScheme == .light ? .black : .black)
                        .padding()
                        .background(
                            LinearGradient(gradient: Gradient(colors: [deviceColorScheme == .light ? .gray : .white, deviceColorScheme == .light ? .yellow : .yellow]), startPoint: .top, endPoint: .bottom)
                        )
                        .cornerRadius(10)
                    
                    Text("")
                    ForEach(homeWidgetConfigurations, id: \.id) { image in
                        NavigationLink(value: image) {
                            WidgetsView(widget: image)
                                .matchedTransitionSource(id: image.id, in: namespace, configuration: { source in
                                    source
                                        .background(Color.yellow)
                                        .clipShape(RoundedRectangle(cornerRadius: 20))
                                })
                        }
                        .clipShape(RoundedRectangle(cornerRadius: 10))
                        .shadow(radius: 10)
                        .shadow(radius: 15)
                        .shadow(radius: 20)
                        .shadow(radius: 20)
                        .shadow(radius: 20)
                        .shadow(radius: 20)
                        .shadow(radius: 20)
                        .padding(.horizontal, 7)
                    }
                }
                .navigationDestination(for: HomeViewWidgetItem.self) { item in
                    DismissableView {
                        if (item.title == "Entertainment"){
                            EntertainmentView()
                                .navigationTransition(.zoom(sourceID: item.id, in: namespace))
                        }
                        else if (item.title == "News"){
                            NewsHomeView()
                                .navigationTransition(.zoom(sourceID: item.id, in: namespace))
                        }
                        else{
                            Text ("In Dismissable View")
                        }
                    }
                }
            }
        } //end of VStack
    } //end of Geometry

}

Why did you add so many .shadow modifiers? One is enough.

hmm... try it and you will know why I did what I did.

And. question is not that why I did, point is it breaks swiftUI app.

SwiftUI App crashes while switching orientation
 
 
Q