Ever been using NavigationLink inside a NavigationView and got this deprecation warning in Xcode?
Here's how to fix it!
Before
Let's start with an iOS15 example of a NavigationView
with a .stack
NavigationViewStyle, and a NavigationLink which links to SecondView
when the button is pressed.
import SwiftUI
struct ContentView: View {
@State var showSecondView: Bool = false
var body: some View {
NavigationView {
VStack {
Button {
showSecondView.toggle()
} label: {
Text("Show Second View")
}
NavigationLink(
destination: SecondView(),
isActive: $showSecondView
) {
EmptyView()
}
}
}
.navigationViewStyle(.stack)
}
}
struct SecondView: View {
var body: some View {
VStack {
Text("Hello! I'm the second View")
}
}
}
After
To update for iOS 16 and remove the deprecation warning we can:
- Change
NavigationView
toNavigationStack
- Change
NavigationLink
tonavigationDestination
- Remove
.navigationViewStyle(.stack)
struct ContentView: View {
@State var showSecondView: Bool = false
var body: some View {
NavigationStack {
VStack {
Button {
showSecondView.toggle()
} label: {
Text("Show Second View")
}
.navigationDestination(
isPresented: $showSecondView) {
SecondView()
}
}
}
}
}
Hope that helps anyone else who has hit the same warning!
Top comments (1)
It sure did help me. Thanks :)