blog icon indicating copy to clipboard operation
blog copied to clipboard

How to use Button inside NavigationLink in SwiftUI

Open onmyway133 opened this issue 4 years ago • 2 comments

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

onmyway133 avatar Jul 21 '21 23:07 onmyway133

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

dharaspatel avatar Oct 14 '22 14:10 dharaspatel

@dharaspatel You can also apply buttonStyle directly onto NavigationLink

onmyway133 avatar Oct 14 '22 19:10 onmyway133