7

This bug is driving me insane. Sometimes (well most of the time) presented sheet gets dismissed first time it is opened. This is happening only on a device and only the first time the app is launched. Here is how it looks on iPhone 11 running iOS 14.1 built using Xcode 12.1 (can be reproduced on iPhone 7 Plus running iOS 14.0.1 as well):

enter image description here

Steps in the video:

  1. I open app
  2. Swiftly navigate to Details view
  3. Open Sheet
  4. Red Sheed gets dismissed by the system???
  5. I open sheet again and it remains on the screen as expected.

This is SwitUI App project (using UIKIt App Delegate) and deployment iOS 14. Code:

struct ContentView: View {
   
    var body: some View { 
        NavigationView {
            NavigationLink(destination: DetailsView()) {
                Text("Open Details View")
            }
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

struct DetailsView: View {
   
    @State var sheetIsPresented: Bool = false
 
    var body: some View {
        VStack {
            Button("Open") {
                sheetIsPresented.toggle()
            }
        }.sheet(isPresented: $sheetIsPresented, content: {
            SheetView()
        })
    }
}

struct SheetView: View {
   
    var body: some View {
        Color.red
    }
}

I was able to fix this problem by removing line .navigationViewStyle(StackNavigationViewStyle()), but I need StackNavigationViewStyle in my project. Any help will be appreciated.

Updated: There is a similar post on Apple forum with sheet view acting randomly weird. One solution that I found is to move sheet to the root view outside NavigationLink (that would be ContentView in my example), but that is not ideal solution.

sash
  • 8,423
  • 5
  • 63
  • 74
  • I tried on physical device No problem! test on SE2 – ios coder Nov 08 '20 at 20:05
  • @Omid thanks. Can you please try it multiple times and do all the actions(navigation) quickly? After each try, please force close the app – sash Nov 08 '20 at 20:16
  • I did all I could, send to background and then back to forground, swipe up, down, multi open also, no problem! did you tried on clean device? It happened for me some times in the past, the old project crash put some effect on device – ios coder Nov 08 '20 at 20:21
  • No problems on a physical device or on Simulator using Xcode Version 12.1 (12A7403) – lorem ipsum Nov 08 '20 at 20:36
  • Tested on iPhone 7 Plus (iOS 14.0.1), had the same problem. – sash Nov 09 '20 at 09:06
  • I also encountered this problem, do you have a solution? Xcode 13.3 – IDTIMW Apr 07 '22 at 07:52

2 Answers2

4

I had the same problem in an app. After a great deal of research, I found that making the variable an observed object fixed the problem in SwiftUI 1, and it seems to be in SwiftUI 2. I do remember that it was an intermittent problem on an actual device, but it always happened in the simulator. I wish I could remember why, maybe when the sheet appears it resets the bound variable?, but this code fixes the problem:

import SwiftUI
import Combine

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: DetailsView()) {
                Text("Open Details View")
            }
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

struct DetailsView: View {
   
    @ObservedObject var sheetIsPresented = SheetIsPresented.shared
    
    var body: some View {
        VStack {
            Button("Open") {
                sheetIsPresented.value.toggle()
            }
        }.sheet(isPresented: $sheetIsPresented.value, content: {
            SheetView()
        })
    }
}

struct SheetView: View {
   
    var body: some View {
        Color.red
    }
}

final class SheetIsPresented: NSObject, ObservableObject {
    let objectWillChange = PassthroughSubject<Void, Never>()
    
    static let shared = SheetIsPresented()
    
    @Published var value: Bool = false {
        willSet {
            objectWillChange.send()
        }
    }
    
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Tested on Xcode 12.1, iOS 14.1 in simulator.

Yrb
  • 8,103
  • 2
  • 14
  • 44
0

Just add your NavigationView view to bellow code line

.navigationViewStyle(StackNavigationViewStyle())

Example:


NavigationView {

}
.navigationViewStyle(StackNavigationViewStyle())

Problem will be solved (same issues I was faced)

gaohomway
  • 2,132
  • 1
  • 20
  • 37