DEV Community

Ceri-anne
Ceri-anne

Posted on

7

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 :)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

đź‘‹ Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay