Take the most basic example app with two screens in a NavigationView
:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
List {
NavigationLink(destination: SubView()) {
Text("Go to screen 2")
}
}
.navigationBarTitle("Screen 1", displayMode: .inline)
}
}
}
struct SubView: View {
var body: some View {
Text("Hello, World!")
.navigationBarTitle("Screen 2", displayMode: .inline)
}
}
If you go to screen 2 and press the back button to go back to screen 1, SubView
is never released. If you go back and forth multiple times, there will be multiple instances of SubView
alive. This feels like a huge problem / memory leak, seems weird it behaves like this by default. Is there a workaround?
release
forstruct
? – XeniaNavigationStack
/NavigationSplitView
instead? – Callahan