Learn Courses My Dashboard

ScrollView prevent Back to work

If the ScrollView size (using GeometryReader) is greater than geo.size.height*0.85 then after navigating to the next screen the Back button does not operate (the app is frozen).

You should provide code of the View in which you have your ScrollView and the screen you are navigating to so that we can see what you are attempting to do.

Paste your code in as text, rather than providing a screenshot.

To format the code nicely, place 3 back-ticks ``` on the line above your code and 3 back-ticks ``` on the line below your code. Like this:

```
Code goes here
```

The 3 back-ticks must be the ONLY characters on the line. The back-tick character is located on the same keyboard key as the tilde character ~ (which is located below the Esc key).

Alternatively after you paste in your code, select that code block and click the </> button on the toolbar. This does the same thing as manually typing the 3 back ticks on the line above and below your code.

This also makes it easier for anyone assisting as they can copy the code and carry out some testing.

//
//  eventView.swift
//  HappyTeen
//
//  Created by Alain Bernay on 28/2/2022.
//

import SwiftUI

struct eventView: View {
    @EnvironmentObject var modelM:MoodModel
    @Environment(\.presentationMode) var presentationMode
    var dbRecords: FetchedResults<DBRecord>
    
    var body: some View {
        GeometryReader { geo in
            NavigationView {
                ZStack {
                    if UIDevice.isIPad {
                        Image("background").resizable().scaledToFill().ignoresSafeArea(.all).opacity(0.3)
                    } else {
                        Image("background").ignoresSafeArea(.all).opacity(0.3)
                    }
                    VStack (alignment: .center) {
                        Spacer()
                        ScrollView (showsIndicators: false) {
                            LazyVGrid(columns: [GridItem(.flexible(), spacing: 4, alignment: .top), GridItem(.flexible(), spacing: 4, alignment: .top)], alignment: .center, spacing: 4) {
                                ForEach (modelM.moodData[0].situations) { event in
                                    singleEvent(event: event)
                                }
                            } // LazzyVGrid
                        }
                        Spacer()
                    }.frame(width: geo.size.width*0.9, height: geo.size.height*0.85)
                    // WARNING: geo.size.height*0.85 is the maximum otherwise it causes problem with backbutton for the next View
                    // Add a custom back button  & forward button
                    .toolbar {ToolbarItem(placement: .navigationBarLeading) {
                        Button(action: {self.presentationMode.wrappedValue.dismiss()}, label: {
                            HStack {Image(systemName: "arrow.left.circle").resizable().scaledToFit().frame(width: 16, height: 16)
                                Text("Back").font(.caption)}})}
                        ToolbarItem (placement: .principal) {
                            HStack {
                                if UIDevice.isIPad {
                                    Text("Who is involved?")
                                        .font(.title)
                                        .padding(2)
                                        .border(Color(.systemGreen), width: 1)
                                        .foregroundColor(Color(.systemBlue))
                                } else {
                                    Text("Who is involved?")
                                        .font(.caption)
                                        .padding(2)
                                        .border(Color(.systemGreen), width: 1)
                                        .foregroundColor(Color(.systemBlue))
                                }
                                if modelM.moodData[0].records[0].overallPositive {
                                    Image(systemName: "face.smiling").resizable().scaledToFit().frame(width: 16, height: 16).background(Color(.systemGreen))
                                } else {
                                    Image(systemName: "person").resizable().scaledToFit().frame(width: 16, height: 16).background(Color(.systemRed))
                                }
                            }
                        }
                        ToolbarItem(placement: .navigationBarTrailing) {
                            NavigationLink (destination: eventDetailsView(dbRecords: dbRecords)
                                    .onAppear(perform: {modelM.initUITableView()}), label: {
                                HStack {Image(systemName: "arrow.right.circle").resizable().scaledToFit().frame(width: 16, height: 16)
                                    Text("Next").font(.caption)}})}}.accentColor(Color(.systemRed))  // VStack
                } // Zstack
            }.navigationViewStyle(.stack).navigationBarBackButtonHidden(true).navigationBarHidden(true) // NavigationView
        } // Geo
    }
}

If size.height is greater than 0.85 when I use the Next button it goes to the eventDetailsView which has a Back button. When pressing the Back button it becomes grey then nothing happens, the app is frozen. I have to kill the app.
This situation is happening also for other Views that have similar codes.