Ios 如何以编程方式使用Swift 3在所有视图控制器中显示选项卡栏?

Ios 如何以编程方式使用Swift 3在所有视图控制器中显示选项卡栏?,ios,swift,swift3,Ios,Swift,Swift3,我不熟悉斯威夫特。如何在不使用情节提要的情况下,以编程方式在swift 3中的所有视图控制器中显示选项卡栏 场景如下所示 1.我有3个视图控制器(例如view1、view2、view3)与选项卡栏相连 2.当我单击view2中的一个按钮时,它会导航到另一个视图控制器(比如view4),而在view4中,选项卡栏不会出现 我在视图控制器(比如homeView)中添加了选项卡栏 这是我在homeView的viewDidLoad()中的代码 // Create Tab one let ho

我不熟悉斯威夫特。如何在不使用情节提要的情况下,以编程方式在swift 3中的所有视图控制器中显示选项卡栏

场景如下所示

1.我有3个视图控制器(例如view1、view2、view3)与选项卡栏相连

2.当我单击view2中的一个按钮时,它会导航到另一个视图控制器(比如view4),而在view4中,选项卡栏不会出现

我在视图控制器(比如homeView)中添加了选项卡栏

这是我在homeView的viewDidLoad()中的代码

// Create Tab one
      let homeviewController = view1()
      let homeTabBarItem = UITabBarItem(title: nil, image: UIImage(named: "iconHome"), selectedImage: UIImage(named: "iconHome@3x"))
      homeviewController.tabBarItem = homeTabBarItem
      homeviewController.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0)

    // Create Tab two
    let categoryviewController = view2()
    let categoryTabBarItem = UITabBarItem(title: nil, image: UIImage(named: "iconBrowse"), selectedImage: UIImage(named: "iconBrowse@3x"))
    categoryviewController.tabBarItem = categoryTabBarItem
    categoryviewController.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0)

    // Create Tab three
    let userprofviewController = view3()
    let userTabBarItem = UITabBarItem(title: nil, image: UIImage(named: "iconProfile@3x"), selectedImage: UIImage(named: "iconProfile"))
    userprofviewController.tabBarItem = userTabBarItem
    userprofviewController.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0)


    UITabBar.appearance().barTintColor = UIColor.black
    UITabBar.appearance().itemPositioning = .fill
    self.viewControllers = [homeviewController, categoryviewController, userprofviewController]
提前感谢

您可以在下面尝试

class CustomTabBarController: UITabBarController,UITabBarControllerDelegate {

    override func viewDidLoad() 
    { 
        super.viewDidLoad()
        self.delegate = self

        viewControllers = [  setViewController( “Tab 1”, imageName: “ic_tab1”), setViewController( "Tab 2”, imageName: “ic_Tab2”) ,   setViewController( "Tab 3”, imageName: "ic_Tab3”)  , setViewController("Tab 4”, imageName: “ic_Tab4”)]
    }

    fileprivate func createDummyNavControllerWithTitle(_ title: String, imageName: String) -> UINavigationController {
        let viewController = UIViewController()
        let navController = UINavigationController(rootViewController: viewController)
        navController.tabBarItem.title = title
        navController.tabBarItem.image = UIImage(named: imageName)
        return navController
    }
    fileprivate func setViewController( _ title: String, imageName: String) -> UINavigationController {
        let layout = UICollectionViewFlowLayout()
        let list =  ListController(collectionViewLayout: layout)
        list.navtTitle = title
        let navController = UINavigationController(rootViewController: list)
          navController.tabBarItem.title = title
        list.isContactsScreen = true
         navController.tabBarItem.image = UIImage(named: imageName)
        return recentMessagesNavController
    }

    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

       // print("Selected item")
    }

    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
      //  print("Selected view controller")
    }
}

不要将ViewController直接带到self.viewController,而是按如下方式带到UINavigationController

    // Create Tab one
    let homeviewController = view1()
    let homeTabBarItem = UITabBarItem(title: nil, image: UIImage(named: "iconHome"), selectedImage: UIImage(named: "iconHome@3x"))
    homeviewController.tabBarItem = homeTabBarItem
    homeviewController.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0)
    let navHome = UINavigationController.init(rootViewController: homeviewController)

    // Create Tab two
    let categoryviewController = view2()
    let categoryTabBarItem = UITabBarItem(title: nil, image: UIImage(named: "iconBrowse"), selectedImage: UIImage(named: "iconBrowse@3x"))
    categoryviewController.tabBarItem = categoryTabBarItem
    categoryviewController.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0)
    let navCategory = UINavigationController.init(rootViewController: categoryviewController)

    // Create Tab three
    let userprofviewController = view3()
    let userTabBarItem = UITabBarItem(title: nil, image: UIImage(named: "iconProfile@3x"), selectedImage: UIImage(named: "iconProfile"))
    userprofviewController.tabBarItem = userTabBarItem
    userprofviewController.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0)
    let navUserPrfl = UINavigationController.init(rootViewController: userprofviewController)


    UITabBar.appearance().barTintColor = UIColor.black
    UITabBar.appearance().itemPositioning = .fill
    self.viewControllers = [navHome, navCategory, navUserPrfl]

谢谢拉胡尔的回复。但是ListController函数和recentMessagesNavController是未定义的ListController应该是您要加载的viewcontroller(在我的例子中,我加载的是collectionview显示列表,所以它被命名为ListController),它应该返回NavControllerHanks Manikanta作为您有价值的回复,而不是recentMessagesNavController,但是,如您所述,使用UINavigationController会出现以下错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“不支持推送导航控制器”Hi Rama Mahapatra,请在初始化Tabbar的地方共享您的代码。