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

时间:2017-07-13 11:27:29

标签: ios swift swift3

我是Swift的新手。如何在不使用故事板的情况下以编程方式在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]

提前致谢

2 个答案:

答案 0 :(得分:2)

不要将viewControllers直接带到self.viewControllers,而是按照以下方式获取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]

答案 1 :(得分:0)

您可以尝试以下

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")
    }
}