Present ViewController from TabBarController

45 views Asked by At

I need to open ReportViewController from tabbar modally. How i can do that? I have this TabBarConfigurator:

import UIKit

final class TabBarConfigurator {

    // MARK: - Private property

    private let allTab: [TabBarModel] = [.explore, .feed, .report, .favorite, .profile]

    // MARK: - Internal Methods

    func configure() -> UITabBarController {
        return getTabBarController()
    }
}

// MARK: - Private Methods

private extension TabBarConfigurator {

    func getTabBarController() -> UITabBarController {
        let tabBarController = UITabBarController()
        tabBarController.tabBar.tintColor = UIColor(named: "Green")
        tabBarController.tabBar.unselectedItemTintColor = UIColor(named: "DarkGrey")
        tabBarController.tabBar.backgroundColor = .white
        tabBarController.viewControllers = getViewControllers()
        return tabBarController
    }

    func getViewControllers() -> [UIViewController] {
        var viewControllers = [UIViewController]()
        allTab.forEach { tab in
            let controller = getCurrentViewController(tab: tab)
            let navigationController = UINavigationController(rootViewController: controller)
            let tabBarItem = UITabBarItem(title: tab.title, image: tab.image, selectedImage: tab.selectedImage)
            controller.tabBarItem = tabBarItem
            controller.navigationController?.navigationBar.prefersLargeTitles = true
            controller.title = tab.title
            controller.view.backgroundColor = UIColor(named: "Background")
            viewControllers.append(navigationController)
        }
        return viewControllers
    }

    func getCurrentViewController(tab: TabBarModel) -> UIViewController {
        switch tab {
        case .explore:
            return ExploreViewController()
        case .feed:
            return ActivityViewController()
        case .report:
            return ReportViewController()
        case .favorite:
            return FavoritesViewController()
        case .profile:
            return ProfileViewController()
        }
    }
} 

And SceneDelegate

        guard let windowScene = (scene as? UIWindowScene) else { return }
        
        window = UIWindow(frame: windowScene.coordinateSpace.bounds)
        window?.windowScene = windowScene
        window?.makeKeyAndVisible()
        
        runMainFlow()
        runLaunchScreen()
    }
    
    func runMainFlow() {
        DispatchQueue.main.async {
            self.window?.rootViewController = TabBarConfigurator().configure()
            // Set the navigation bar title color
            UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor(named: "DarkGrey") ?? .black]
        }
    }
    
}

This is how it looks now. This is how it looks now

But i wand to present it modally .pageSheet. How i can do it? I tried if .report inside getViewController function, but nothing happened.

    func getTabBarController() -> UITabBarController {
    let tabBarController = UITabBarController()
    tabBarController.tabBar.tintColor = UIColor(named: "Green")
    tabBarController.tabBar.unselectedItemTintColor = UIColor(named: "DarkGrey")
    tabBarController.tabBar.backgroundColor = .white
    tabBarController.viewControllers = getViewControllers(to: tabBarController)
    return tabBarController
}

func getViewControllers(to tabBarController: UITabBarController) -> [UIViewController] {
    var viewControllers = [UIViewController]()
    for tab in allTab {
        let controller = getCurrentViewController(tab: tab)
        if tab == .report {
            controller.title = tab.title
            controller.view.backgroundColor = UIColor(named: "Background")
            let tabBarItem = UITabBarItem(title: tab.title, image: tab.image, selectedImage: tab.selectedImage)
            controller.tabBarItem = tabBarItem
            let navigationController = UINavigationController(rootViewController: controller)
            navigationController.modalPresentationStyle = .automatic
            tabBarController.present(navigationController, animated: true, completion: nil)
            viewControllers.append(navigationController)
        }
        if tab == .explore {
            let navigationController = UINavigationController(rootViewController: controller)
            let tabBarItem = UITabBarItem(title: tab.title, image: tab.image, selectedImage: tab.selectedImage)
            controller.tabBarItem = tabBarItem
            controller.navigationController?.navigationBar.prefersLargeTitles = true
            controller.title = tab.title
            controller.view.backgroundColor = UIColor(named: "Background")
            viewControllers.append(navigationController)
        }
        if tab == .favorite {
            let navigationController = UINavigationController(rootViewController: controller)
            let tabBarItem = UITabBarItem(title: tab.title, image: tab.image, selectedImage: tab.selectedImage)
            controller.tabBarItem = tabBarItem
            controller.navigationController?.navigationBar.prefersLargeTitles = true
            controller.title = tab.title
            controller.view.backgroundColor = UIColor(named: "Background")
            viewControllers.append(navigationController)
        }
        if tab == .feed {
            let navigationController = UINavigationController(rootViewController: controller)
            let tabBarItem = UITabBarItem(title: tab.title, image: tab.image, selectedImage: tab.selectedImage)
            controller.tabBarItem = tabBarItem
            controller.navigationController?.navigationBar.prefersLargeTitles = true
            controller.title = tab.title
            controller.view.backgroundColor = UIColor(named: "Background")
            viewControllers.append(navigationController)
        }
        if tab == .profile {
            let navigationController = UINavigationController(rootViewController: controller)
            let tabBarItem = UITabBarItem(title: tab.title, image: tab.image, selectedImage: tab.selectedImage)
            controller.tabBarItem = tabBarItem
            controller.navigationController?.navigationBar.prefersLargeTitles = true
            controller.title = tab.title
            controller.view.backgroundColor = UIColor(named: "Background")
            viewControllers.append(navigationController)
        }
    }
    return viewControllers
}

This is how it looks after my changes: enter image description here

And this is how it should look like, after tapping on report tab in tabbar enter image description here

Thank you all for answers!

0

There are 0 answers