DEV Community

Ceri-anne
Ceri-anne

Posted on

Navigation Link - Swift UI iOS 16

Ever been using NavigationLink inside a NavigationView and got this deprecation warning in Xcode?

Xcode deprecation warning - 'init(destination:isActive:label:)' was deprecated in iOS 16.0: use NavigationLink(value🏷️) inside a NavigationStack or NavigationSplitView

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")
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

After

To update for iOS 16 and remove the deprecation warning we can:

  • Change NavigationView to NavigationStack
  • Change NavigationLink to navigationDestination
  • 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()
                 }
            }
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Hope that helps anyone else who has hit the same warning!

Top comments (1)

Collapse
 
mdidehbanmehr profile image
mdidehbanmehr

It sure did help me. Thanks :)