Development environment: Xcode 16.1, macOS 15.1 (24B83)
OS version: iOS iOS 17.5 and above
When NavigationStack
is presented by a sheet, and navigationDestination
API is being used for navigation within the stack, the sheet content is forced to be reevaluated when app is in background.
Sample Project
import SwiftUI
struct TestView: View {
var id: Int
@State private var isLoading = false
@State private var presentSheet = false
var body: some View {
let _ = Self._printChanges()
VStack {
if isLoading {
ProgressView()
} else {
VStack {
Text("View: \(id)")
Text("Not loading")
Button("Present Sheet in NavStack") {
presentSheet = true
}
NavigationLink("Show Link", value: id * 100)
}
}
}
.sheet(
isPresented: $presentSheet,
content: {
NavigationStack {
TestView(id: self.id + 1)
// Comment this block out and note the view no longer reloads when app is in background when there's > 1 sheet modals
.navigationDestination(for: Int.self, destination: { num in
TestView(id: num)
})
}
}
)
.task {
isLoading = true
defer {
isLoading = false
}
try? await Task.sleep(for: .seconds(1))
}
}
}
struct ContentView: View {
var body: some View {
let _ = Self._printChanges()
VStack {
content
.padding()
}
}
var content: some View {
VStack {
NavigationStack {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
TestView(id: 0)
}
.navigationDestination(for: Int.self, destination: { num in
TestView(id: num)
})
}
}
}
}
Steps to reproduce: To reproduce the issue in the sample project:
- Tap on present sheet in navstack button to invoke View #1 presented in sheet
- Put the app into multitasking mode by tapping on the simulator home button twice
- Observe that console does not emit debug messages on SwiftUI View change. Also observe that there was no loading in the view
- Tap on present sheet in navstack button again to invoke View #2 presented in sheet
- Repeat step #2, but this time observe that console does emit debug message that SwiftUI View were changed due to the following:
- TestView: @self, @identity, _isLoading, _presentSheet changed.
- TestView: _isLoading changed.
- TestView: _isLoading changed.
To remove the issue:
7. Comment out the block on navigationDestination
. Recompile the app and run it in simulator
8. Repeat step 2-5. Note this time the console does not emit debug message that SwiftUI View were changed.