I'm running into some weird behavior, trying to get a simple modal to pop after it has been dismissed.
I have an Add button in the NavigationBar that pops a modal. The modal has a button that will dismiss it, which works. However, I cannot interact with the Add button in the NavigationBar again until I interact with something else on the screen, such as scrolling the List below.
I have also placed another Add button, just for kicks, in the List itself, which always works.
Here's the code for the main view:
import SwiftUI
struct ContentView: View {
  @State var displayModal: Bool = false
  
  var body: some View {
    NavigationView {
      List {
        Text("Hello again.")
          Button(action: { self.displayModal = true }) {
            Text("Add")
          }
      }
      .sheet(isPresented: $displayModal) {
        Modal(isPresented: self.$displayModal)
      }
      .navigationBarTitle("The Title")
      .navigationBarItems(trailing: Button(action: { self.displayModal = true }) {
        Text("Add")
      })
    }
  }
}
And the modal, for completeness:
import SwiftUI
struct Modal: View {
  @Binding var isPresented: Bool
  
  var body: some View {
    VStack {
      HStack {
        Button(action: {
          self.isPresented = false
        }) {
          Text("Cancel")
        }
        .padding()
        Spacer()
      }
      Text("I am the modal")
      Spacer()
    }
  }
}
The only thing I can think of is that something invisible is preventing me from working with the NavigationBar button. So I fired up the UI Debugger, and here's what the ContentView looks like. Note the NavigationBar button.

Now, after I tap the button and display the modal, and then use the UI Debugger to see the ContentView again, all the same elements are in place, but the Button parent views are offset a bit, like this:

Once I drag the List up and down, the UI Debugger shows a view hierarchy identical to the first image.
Does anyone have any idea what's going on here?
I'm using Xcode 11.2.1 and iOS 13 on an iPhone 11 Pro simulator, but have also observed this on my iPhone.
It is really a bug. The interesting thing is that after 'drag to dismiss' the issue is not observed, so it is a kind of 'sync/async' state changing or something.
Workaround (temporary of course, decreases visibility almost completely)
  .navigationBarItems(trailing: Button(action: { self.displayModal = true }) {
    Text("Add").padding([.leading, .vertical], 4)
  })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With