Skip to content

Instantly share code, notes, and snippets.

@Aakashcs
Created July 16, 2020 00:25
Show Gist options
  • Save Aakashcs/a9601d1612eb60b0fd4c5c4e671c89fe to your computer and use it in GitHub Desktop.
Save Aakashcs/a9601d1612eb60b0fd4c5c4e671c89fe to your computer and use it in GitHub Desktop.
NavigateModifier for navigating in swiftUI
extension View {
/// Navigate to a new view.
/// - Parameters:
/// - view: View to navigate to.
/// - binding: Only navigates when this condition is `true`.
func navigate<SomeView: View>(to view: SomeView, when binding: Binding<Bool>) -> some View {
modifier(NavigateModifier(destination: view, binding: binding))
}
}
// MARK: - NavigateModifier
fileprivate struct NavigateModifier<SomeView: View>: ViewModifier {
// MARK: Private properties
fileprivate let destination: SomeView
@Binding fileprivate var binding: Bool
// MARK: - View body
fileprivate func body(content: Content) -> some View {
NavigationView {
ZStack {
content
.navigationBarTitle("")
.navigationBarHidden(true)
NavigationLink(destination: destination
.navigationBarTitle("")
.navigationBarHidden(true),
isActive: $binding) {
EmptyView()
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment