0

Using Swift5.3.2, iOS14.4.1, XCode12.4,

I am showing a modal sheet in SwiftUI.

Unfortunately, the sheet completely freezes whenever an underlying parent-View re-renders/updates.

I am absolutely clueless on how I can circumvent that issue. Any ideas ?

Here is the parent View:

import SwiftUI

struct ParentTrialView: View {
    
    @State var currentDate = Date()
    @State private var showingGrid = false
    let timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect()
        
    var body: some View {
        
        NavigationView {
            Text("\(currentDate)")
                .onReceive(timer) { input in
                    currentDate = input
                }
                .toolbar {
                    ToolbarItem(placement: .navigationBarTrailing) {
                        Button(action: {
                            showingGrid.toggle()
                        }) {
                            Image(systemName: "square.grid.3x3")
                        }
                        .sheet(isPresented: $showingGrid) {                            
                            ChildTrialView(onDismiss: {
                                showingGrid = false
                            })
                        }
                    }
                }
        }
    }
}

And here is the child View:

import SwiftUI

struct ChildTrialView: View {
    
    var onDismiss: () -> ()
    
    var body: some View {
        
        NavigationView {
            Text("Trial")
                .onTapGesture {                    
                    onDismiss()
                }
                .toolbar {
                    ToolbarItem(placement: .navigationBarTrailing) {
                        Button(action: {
                            onDismiss()
                        }) {
                            Text("Cancel")
                        }
                    }
                }
        }
        
    }
}

If you, for example, set the timer to 20 seconds. Then you have 20 seconds where everything works on the child View (i.e. it's tap-Gesture works, and also its Cancel button to close the child-View works perfectly).

However, when the timer fires an update to the parent-View, then no action works on the child-View anymore !!

I am more than desperate with this SwiftUI problem.

Any solution on this ?

iKK
  • 6,394
  • 10
  • 58
  • 131
  • I can't reproduce it with the code you posted on the simulator (XCode 12.2 / iOS 14.2) – New Dev Mar 22 '21 at 13:30
  • 1
    How is what you posted a reproducible example? There are ton of code missing from it. And it's far from minimal. For debugging, you need to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – New Dev Mar 22 '21 at 20:17
  • @New Dev: could you please take out the -1 again since I took quite an effort to explain the problem with a minimal reproducible example now. Thank you. – iKK Mar 22 '21 at 20:48
  • 1
    I wasn't the one who downvoted... but the simplified example is better, so happy to upvote. But I'm still not seeing "freezing" as a result; I could swipe-down for example. – New Dev Mar 22 '21 at 21:54
  • 1
    I think the weird part is having `.sheet` hanging off of the child of a toolbar. If you placed it under `Text("\(currentDate)")` or under `NavigationView`, it should work then for you – New Dev Mar 22 '21 at 21:56
  • yes, wow - thank you very much ! This was it ! Placing the .sheet directly at `Text("\(currentDate)")` made it work. Thank you very much for your patience and fast responses. – iKK Mar 22 '21 at 22:21
  • No problem.. That's the power of a minimal reproducible example. – New Dev Mar 22 '21 at 22:21

0 Answers0