Created
July 16, 2020 00:25
-
-
Save Aakashcs/a9601d1612eb60b0fd4c5c4e671c89fe to your computer and use it in GitHub Desktop.
NavigateModifier for navigating in swiftUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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