0

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

    }

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
Attila Marosi
  • 144
  • 1
  • 8
  • Thank you for your answer Julian. Unfortunately it drops an error: "Tried to pop to a view controller that doesn't exist" – Attila Marosi Jan 28 '19 at 17:19

1 Answers1

1

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

Nishant Pathania
  • 349
  • 1
  • 14
  • Thanks, but we don't use storyboard. Anyway, with this code, the navigation controller becomes visible with a back button, but tabBar is missing at the bottom. self.navigationController?.pushViewController(SearchResultsVC(), animated: true) – Attila Marosi Jan 28 '19 at 17:23
  • Should I use UITabBarControllerDelegate? – Attila Marosi Jan 28 '19 at 17:25
  • I think this might be it i have implemented it myself: using an array of controllers:--https://medium.com/@ITZDERR/uinavigationcontroller-and-uitabbarcontroller-programmatically-swift-3-d85a885a5fd0 – Nishant Pathania Jan 28 '19 at 17:26
  • Thanks Mayank, that's what I'm looking for! – Attila Marosi Jan 28 '19 at 17:29
  • no worries man happy to help. A small favour would you please upvote – Nishant Pathania Jan 28 '19 at 17:30
  • A little addition to the topic: Subclass UINavigationController: class CustomNavigationController: UINavigationController { override func viewDidLoad() { super.viewDidLoad() // Your customization code goes here... navigationBar.barTintColor = .magenta let titleAttribute = [NSAttributedString.Key.foregroundColor: UIColor.white] navigationBar.titleTextAttributes = titleAttribute navigationBar.tintColor = .white } } Then use it within TabBar let firstVC = CustomNavigationController(rootViewController: FirstVC()) – Attila Marosi Jan 28 '19 at 19:53