Hi, I've recently experienced a weird crash that only happening on iOS 16 device. When dismiss a sheet which contains NavigationView it sometime crashes. It is not 100% reproducible but relatively easy to get crash. The same build running on iOS 15 device is totally fine. The crash report shows nothing useful and the stack trace shows none of my code so I'm not sure what's going on in here. Any help is much appreciated.
iOS 16 crash on dismiss sheet with navigationView
Here's a work around that is working for me.
struct NavStackWorkaround<T: View>: View {
let content: ()->T
var body: some View {
if #available(iOS 16, *) {
NavigationStack(root: content)
} else {
NavigationView(content: content)
.navigationViewStyle(.stack)
}
}
}
Simply replace your NavigationView uses with NavStackWorkaround (and remove the .navigationViewStyle)
Hi, your workaround works but the navigationLinks (with isActive param) are not working Actually They are working once. It is like the state is not set again when you goback previous screen
I am experiencing this crash, but we use UIKit navigation with HostViewControllers + SwiftUI Views on them. Any thoughts on workarounds? Thanks
We found this issue on certain iPhones running iOS 16 and in the end we realised we had a function written like so:
// Bad code!
fun doButtonAction() {
DispatchQueue.main.async { [weak self] in
self?.sheetItem = .none
}
}
// SwiftUI View below
Button {
doButtonAction()
} label : {
Text("Press me!")
}
And what fixed it for us was:
@MainActor fun doButtonAction() {
sheetItem = .none
}
The same crash occured in the app i'm working on. Only on iOS 16. It seems like the reason for the crash is pushing a new view before the last view dismisses. In my case, it was dismissing a fullscreen cover and changing a tab in a tabview. Workaround that worked for me was,
Dismiss the cover view, and it's onDisappear
change the tab. (Keep a state variable so tab change doesnt happen in every onDisappear
)