blog
blog copied to clipboard
How to use Button inside NavigationLink in SwiftUI
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())
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 You can also apply buttonStyle directly onto NavigationLink