Skip to content

Instantly share code, notes, and snippets.

@DarrenHurst
Created April 19, 2021 17:56
Show Gist options
  • Save DarrenHurst/515f89673d890e771668b55252f4cb3c to your computer and use it in GitHub Desktop.
Save DarrenHurst/515f89673d890e771668b55252f4cb3c to your computer and use it in GitHub Desktop.
//
// Router.swift
// test
//
// Created by Darren Hurst on 2021-04-15.
//
// Usage Example
// Router(isActive: $info, route: AnyView(SettingsView()), label: { _ in
// Text("Get an Account")
// })
//
// or
// Hidden Route
// Router(isActive: $accountDetailRoute, route: AnyView(AccountDetails()), label: { _ in }).hidden().frame(width: 0, height: 0)
//
import Foundation
import SwiftUI
protocol RouterProtocol {
var route: AnyView {get set}
}
struct Router<Content: View>: View, RouterProtocol {
var route: AnyView
@State var isActive: Bool = false
var label: Content
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
init(route: AnyView, @ViewBuilder label:(()?) -> Content ) {
self.route = route
self.label = label(())
self.isActive = isActive
}
var body: some View {
NavigationLink(
destination: route,
isActive: $isActive,
label: {
label
}
)
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading:
VStack {
Button(action: {
presentationMode.wrappedValue.dismiss()
}, label: {
Image(systemName: "arrowshape.turn.up.left.circle").foregroundColor(Color.white).frame(width: 25, height: 25, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
})
}.frame(height:344).foregroundColor(.white)
).ignoresSafeArea()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment