以编程方式在 UITabBarController 和 UINavigationController 中呈现视图

Present View within UITabBarController & UINavigationController Programmatically

我们正在开发一个 iOS 应用程序,它有一个 RootVC,其中以编程方式放置了 4 个 TAB,每个 Tab 都有一个单独的 ViewController。负责搜索的选项卡之一。当用户点击此特定 ViewController 上的搜索按钮时,我们希望在另一个 ViewController 中显示搜索结果,其中 TabBar 在底部,NavigationController 在顶部带有 "Back" 按钮。 我怎样才能做到这一点?我尝试使用 self.navigationController?.present、推送,但其中 none 有效。

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

    }

试试这个

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

navigationController?.pushViewController(vc, animated: true)

然后观察是否出现view not in hierarchy之类的错误