Skip to content

Instantly share code, notes, and snippets.

@kvdesa
Last active July 20, 2022 21:09
Show Gist options
  • Save kvdesa/9a35598706270e48c2e532e2cf9d1ece to your computer and use it in GitHub Desktop.
Save kvdesa/9a35598706270e48c2e532e2cf9d1ece to your computer and use it in GitHub Desktop.
ViewController with custom actions for showing/hiding the navigation bar and tab bar.
class CustomBarDisplayViewController: UIViewController {
// MARK: - Enums
enum NavBarDisplayAction {
case show
case hide
case none
}
struct TabBarDisplayAction {
enum ActionType {
case show
case hide
}
enum Trigger {
case willAppear
case didAppear
case willDisappear
case didDisappear
}
let type: ActionType
let trigger: Trigger
let isAnimated: Bool
}
// MARK: - Properties
var navBarDisplayActionOnAppear = NavBarDisplayAction.none
var navBarDisplayActionOnDisappear = NavBarDisplayAction.none
private var tabBarDisplayActions = [TabBarDisplayAction]()
// MARK: - Actions
func addTabBarDisplayAction(_ type: TabBarDisplayAction.ActionType,
trigger: TabBarDisplayAction.Trigger,
isAnimated: Bool = false) {
tabBarDisplayActions.append(TabBarDisplayAction(type: type, trigger: trigger, isAnimated: isAnimated))
}
// MARK: - Life-cycle
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let navigationController = navigationController {
switch navBarDisplayActionOnAppear {
case .show:
guard navigationController.isNavigationBarHidden else { return }
navigationController.setNavigationBarHidden(false, animated: animated)
case .hide:
guard !navigationController.isNavigationBarHidden else { return }
navigationController.setNavigationBarHidden(true, animated: animated)
case .none:
break
}
}
if let tabBarController = tabBarController,
let action = tabBarDisplayActions.first(where: { $0.trigger == .willAppear }) {
tabBarController.setTabBarHidden(action.type == .hide, animated: action.isAnimated)
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let tabBarController = tabBarController,
let action = tabBarDisplayActions.first(where: { $0.trigger == .didAppear }) {
tabBarController.setTabBarHidden(action.type == .hide, animated: action.isAnimated)
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if let navigationController = navigationController {
switch navBarDisplayActionOnDisappear {
case .show:
guard navigationController.isNavigationBarHidden else { return }
navigationController.setNavigationBarHidden(false, animated: animated)
case .hide:
guard !navigationController.isNavigationBarHidden else { return }
navigationController.setNavigationBarHidden(true, animated: animated)
case .none:
break
}
}
if let tabBarController = tabBarController,
let action = tabBarDisplayActions.first(where: { $0.trigger == .willDisappear }) {
tabBarController.setTabBarHidden(action.type == .hide, animated: action.isAnimated)
}
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
if let tabBarController = tabBarController,
let action = tabBarDisplayActions.first(where: { $0.trigger == .didDisappear }) {
tabBarController.setTabBarHidden(action.type == .hide, animated: action.isAnimated)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment