|
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) |
|
} |
|
} |
|
} |