onmyway133 / blog

🍁 What you don't know is what you haven't learned
https://onmyway133.com/
MIT License
669 stars 33 forks source link

How to use Button inside NavigationLink in SwiftUI #826

Open onmyway133 opened 2 years ago

onmyway133 commented 2 years ago

Use isActive binding

@State private var goesToDetail: Bool = false

NavigationLink(
    destination: DetailView(viewModel: viewModel),
    isActive: $goesToDetail) {
    Button(action: { goesToDetail = true }) {
        Text("Next")
    }
    .buttonStyle(MyButtonStyle))
}

We can also apply buttonStyle onto NavigationLink

NavigationLink {
    FavoritesView()
} label: {
    Image(systemImage: "star.fill")
}
.buttonStyle(MyButtonStyle())
dharaspatel commented 1 year ago

I have been looking forever for a solution and so glad I found this. So sad to see that isActive: $... is being deprecated in iOS 16.0. Any solutions that work with iOS16.0?

Full warning: 'init(destination:isActive:label:)' was deprecated in iOS 16.0: use NavigationLink(value:label:) inside a NavigationStack or NavigationSplitView

onmyway133 commented 1 year ago

@dharaspatel You can also apply buttonStyle directly onto NavigationLink