Skip to content

Commit

Permalink
Merge pull request #42 from nunocaseiro/feat/backbutton-navigationbar…
Browse files Browse the repository at this point in the history
…-visibility

feat: shows/hides back button and navigation bar
  • Loading branch information
erikdrobne authored Jul 19, 2024
2 parents 2112615 + 06266c9 commit 1bbbcd0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Sources/SwiftUICoordinator/Navigator/Navigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down
12 changes: 12 additions & 0 deletions Sources/SwiftUICoordinator/Routing/Route/NavigationRoute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -36,4 +40,12 @@ public extension NavigationRoute {
var action: TransitionAction? {
return nil
}

var hidesBackButton: Bool? {
return nil
}

var hidesNavigationBar: Bool? {
return nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,9 @@ public class RouteHostingController<Content: View>: UIHostingController<Content>
if let appearance = route.appearance {
self.view.backgroundColor = appearance.background
}

if let hidesBackButton = route.hidesBackButton {
self.navigationItem.setHidesBackButton(hidesBackButton, animated: false)
}
}
}

0 comments on commit 1bbbcd0

Please sign in to comment.