Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI modal sheet dismisses itself after half a second

Tags:

ios

swift

swiftui

I have got a modal sheet, here is the code:

SettingsDashboardView:

   @State private var notificationsSettingsSheet = false

    
    var body: some View {
        
        Button(action: {
            self.notificationsSettingsSheet.toggle()
        }) {
            
            VStack(alignment: .leading) {
                HStack(alignment: .top, spacing: 4) {
                    Label("Set Daily Reminders", systemImage: "alarm").foregroundColor(Color("TextColor"))
                        .font(.system(.headline, design: .rounded))
                    
                    Spacer()
                }
                

            }
        }
        .sheet(isPresented: $notificationsSettingsSheet) {
            NotificationSettingsModal()
        } 
    }

NotificationSettingsModal:

var body: some View {
        ZStack(alignment: .bottom) {
            ScrollView {
                
                VStack(alignment: .leading, spacing: 0) {
                    
    
                    Text("Daily Reminders")
                        .font(.system(.title, design: .rounded))
                    .fontWeight(.bold)
                    .padding(.top, headingTopPadding)
                    .padding(.horizontal, headingHorizontalPadding).foregroundColor(Color("TextColor"))
             
                    Spacer().frame(height: 164)

                }.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
                
                
                
                Spacer().frame(height: 64)
            }
        }.background(Color("BackgroundColor").edgesIgnoringSafeArea(.all))
    }

When I launch the app and open my sheet, in about 50% of cases sheet dismisses itself after about half a second. If I open sheet after that everything works fine. What can cause this problem?

like image 275
Hekes Pekes Avatar asked Oct 10 '20 21:10

Hekes Pekes


1 Answers

This will probably not solve the mentioned issue but can be useful for others.

In most cases, this issue happens when the view gets redrawn due to a change in some variables. Be careful that it might be the parent view that have some variables changes.

The best way to debug this kind of behaviour is to use the technique describe here, on Hacking with Swift. The idea is to identify what change caused a view to reload itself by printing print(Self._printChanges()) inside the body property. Note that by doing it, you will temporarily need to add an explicit return.

Then, observer the console and it most cases you will be able to identify the issue and refactor your code.

In my experience (does not seem to be the case here) this often happens when using @Environment(\.editMode) var editMode in both the view and parent view. For some reasons this value changes in both views when presenting a sheet, causing the view to be redrawn and the sheet closed.

like image 128
Tim Avatar answered Nov 14 '22 17:11

Tim