From 06266c91ce8a975336bb01fd31517cfa9cd75ac3 Mon Sep 17 00:00:00 2001 From: nunocaseiro Date: Thu, 18 Jul 2024 11:27:38 +0100 Subject: [PATCH] feat: shows/hides back button and navigation bar --- Sources/SwiftUICoordinator/Navigator/Navigator.swift | 8 ++++---- .../Routing/Route/NavigationRoute.swift | 12 ++++++++++++ .../Routing/RouteHostingController.swift | 4 ++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Sources/SwiftUICoordinator/Navigator/Navigator.swift b/Sources/SwiftUICoordinator/Navigator/Navigator.swift index a4fc902..0e5d465 100644 --- a/Sources/SwiftUICoordinator/Navigator/Navigator.swift +++ b/Sources/SwiftUICoordinator/Navigator/Navigator.swift @@ -60,8 +60,8 @@ public extension Navigator where Self: RouterViewFactory { func show(route: Route) throws { let viewController = self.hostingController(for: route) - navigationController.isNavigationBarHidden = route.title == nil - + navigationController.isNavigationBarHidden = route.title == nil && route.hidesNavigationBar ?? true + switch route.action { case .push(let animated): navigationController.pushViewController(viewController, animated: animated) @@ -80,13 +80,13 @@ public extension Navigator where Self: RouterViewFactory { func set(routes: [Route], animated: Bool = true) { let views = views(for: routes) - navigationController.isNavigationBarHidden = routes.last?.title == nil + navigationController.isNavigationBarHidden = routes.last?.title == nil && routes.last?.hidesNavigationBar ?? true navigationController.setViewControllers(views, animated: animated) } func append(routes: [Route], animated: Bool = true) { let views = views(for: routes) - navigationController.isNavigationBarHidden = routes.last?.title == nil + navigationController.isNavigationBarHidden = routes.last?.title == nil && routes.last?.hidesNavigationBar ?? true navigationController.setViewControllers(self.viewControllers + views, animated: animated) } diff --git a/Sources/SwiftUICoordinator/Routing/Route/NavigationRoute.swift b/Sources/SwiftUICoordinator/Routing/Route/NavigationRoute.swift index 5e75e5d..ec40f98 100644 --- a/Sources/SwiftUICoordinator/Routing/Route/NavigationRoute.swift +++ b/Sources/SwiftUICoordinator/Routing/Route/NavigationRoute.swift @@ -18,6 +18,10 @@ public protocol NavigationRoute { var action: TransitionAction? { get } /// A property that indicates whether the Coordinator should be attached to the View as an EnvironmentObject. var attachCoordinator: Bool { get } + /// A property that hides the back button during navigation + var hidesBackButton: Bool? { get } + /// A property that hides the navigation bar + var hidesNavigationBar: Bool? { get } } public extension NavigationRoute { @@ -36,4 +40,12 @@ public extension NavigationRoute { var action: TransitionAction? { return nil } + + var hidesBackButton: Bool? { + return nil + } + + var hidesNavigationBar: Bool? { + return nil + } } diff --git a/Sources/SwiftUICoordinator/Routing/RouteHostingController.swift b/Sources/SwiftUICoordinator/Routing/RouteHostingController.swift index 1aa8a39..ab86815 100644 --- a/Sources/SwiftUICoordinator/Routing/RouteHostingController.swift +++ b/Sources/SwiftUICoordinator/Routing/RouteHostingController.swift @@ -36,5 +36,9 @@ public class RouteHostingController: UIHostingController if let appearance = route.appearance { self.view.backgroundColor = appearance.background } + + if let hidesBackButton = route.hidesBackButton { + self.navigationItem.setHidesBackButton(hidesBackButton, animated: false) + } } }