Attila Marosi
Attila Marosi

Reputation: 174

Present View within UITabBarController & UINavigationController Programmatically

We are working on an iOS app, which has a RootVC, where 4 TABs placed programmatically, each Tab has a separate ViewController. One of the Tabs responsible for search. When the user taps the search button on this specific ViewController, we'd like to present the search results in another ViewController, which has the TabBar at the bottom and the NavigationController at the top with a "Back" button. How can I achieve this? I tried with self.navigationController?.present, push, but none of them worked.

RootVC.swift:


class RootVC: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupTabBarLayout()

    }

    private func setupTabBarLayout() {
        // 1. Profile page
        let profileVC = ProfileVC()
        let profileVCBarItem = UITabBarItem(title: "Profil", image: UIImage(named: "profile_icon"), tag: 1)
        profileVC.tabBarItem = profileVCBarItem

        // 2. Search
        let searchVC = SearchVC()
        let searchVCBarItem = UITabBarItem(title: "Search", image: UIImage(named: "search_icon"), tag: 2)
        searchVC.navigationItem.leftBarButtonItem = nil
        searchVC.tabBarItem = searchVCBarItem

        // 3. Meet
        let meetVC = MeetVC()
        let meetVC = SearchResultsVC()
        let meetVCBarItem = UITabBarItem(title: "Meet", image: UIImage(named: "meet_icon"), tag: 3)
        meetVC.tabBarItem = meetVCBarItem

        // 4. Activities
        let activitiesVC = ActivitiesVC()
        let activitiesVCBarITem = UITabBarItem(title: "Activities", image: UIImage(named: "activities_icon"), tag: 4)
        activitiesVC.tabBarItem = activitiesVCBarITem

        // VC Setup
        viewControllers = [profileVC, searchVC, meetVC, activitiesVC]
        // Design settings
        self.tabBar.backgroundColor = .lightButtonBg
        self.tabBar.barTintColor = .darkMagenta
        self.tabBar.tintColor = .customWhite
        self.tabBar.unselectedItemTintColor = .lightButtonBg
        self.tabBar.isTranslucent = false

    }

Upvotes: 0

Views: 183

Answers (1)

Nishant Pathania
Nishant Pathania

Reputation: 349

Try this

var vc = storyboard?.instantiateViewController(withIdentifier: "identifierForStoryboard

navigationController?.pushViewController(vc, animated: true)

and then observe if there is an error like view not in hierarchy

Upvotes: 1

Related Questions