private var window: UIWindow
private var rootNavigationController: UINavigationController!
init( _ window: UIWindow) {
self.window = window
self.initNavigation()
}
private func initNavigation() {
rootNavigationController = UINavigationController()
rootNavigationController.setNavigationBarHidden(true, animated: false)
window.rootViewController = rootNavigationController
window.makeKeyAndVisible()
window.backgroundColor = .white
}
private func goBackToViewController(_ navigation: UINavigationController, _ viewController: UIViewController) -> Bool {
for controller in navigation.viewControllers {
if controller.isKind(of: type(of: viewController)) {
navigation.popToViewController(controller, animated: true)
return true
}
}
return false
}
private func showViewControllerByMethod(_ navigation: UINavigationController, _ viewController: UIViewController, _ method: ViewControllerShowMethod) {
switch(method) {
case .normal:
navigation.pushViewController(viewController, animated: true)
case .modal:
navigation.present(viewController, animated: true)
}
}
private func show(navigation: UINavigationController, _ viewController: UIViewController, method: PageShowMethod) {
DispatchQueue.main.async {
if(self.goBackToViewController(navigation, viewController)) {
return
}
navigation.view.endEditing(true)
self.showViewControllerByMethod(navigation, viewController, method)
}
}
private func getController(storyboard storyboardName: String, controllerName: String? = nil) -> UIViewController? {
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
let controller = controllerName == nil ?
storyboard.instantiateInitialViewController() :
storyboard.instantiateViewController(withIdentifier: controllerName!)
return controller
}
func showFirstViewController() {
var viewControllers = [UIViewController]()
let firstTabController = getController(storyboard: "First") as! FirstViewController
let firstTabNavigation = UINavigationController(rootViewController: firstTabController)
firstTabNavigation.setNavigationBarHidden(true, animated: false)
firstTabNavigation.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "home-icon"), selectedImage: UIImage(named: "home-filled-icon"))
viewControllers.append(firstTabNavigation)
let secondTabController = getController(storyboard: "Second") as! SecondViewController
let secondTabNavigation = UINavigationController(rootViewController: secondTabController)
secondTabNavigation.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "second-icon"), selectedImage: UIImage(named: "second-filled-icon"))
viewControllers.append(secondTabNavigation)
let tabBarController = BasicTabContainerViewController()
tabBarController.viewControllers = viewControllers
rootNavigationController.viewControllers.insert(tabBarController, at: 0) // Это на тот случай если у нас ранее были viewcontrollers и от них нужно уйти - к примеру контроллеры авторизации
show(navigation: rootNavigationController, tabBarController, method: .normal) // А это чтобы с предыдущих контроллеров возвратиться с анимацией на этот
}
func basicContainerActiveNavigationController() -> UINavigationController? {
var tabBarController: BasicTabContainerViewController? = nil
for controller in rootNavigationController.viewControllers {
if (controller.isKind(of: BasicContainerViewController.self)) {
tabBarController = controller as? BasicContainerViewController
}
}
return tabBarController?.selectedViewController as? UINavigationController
}
func showNewController() {
let viewController = getController(storyboard: "New") as! NewViewController
guard let navigationCotnroller = basicContainerActiveNavigationController() else {
return
}
show(navigation: navigationCotnroller, viewController, method: .normal)
}