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

Posted

技术标签:

【中文标题】如何以编程方式使用 Swift 3 在所有视图控制器中显示选项卡栏?【英文标题】:How to show Tab bar in all view controllers using Swift 3 programmatically? 【发布时间】:2017-07-13 11:27:28 【问题描述】:

我是 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]

提前致谢

【问题讨论】:

***.com/questions/27422054/… 【参考方案1】:

不直接把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]

【讨论】:

感谢 Manikanta 的宝贵回复,但是通过使用 UINavigationController 正如您提到的,出现以下错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“不支持推送导航控制器”跨度> 嗨,Rama Mahapatra,请在初始化 Tabbar 的位置分享您的代码。【参考方案2】:

你可以试试下面的

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

【讨论】:

感谢拉胡尔的回复。但是 ListController 函数和 recentMessagesNavController 是未定义的 ListController 应该是您要加载的视图控制器(在我的情况下,我正在加载显示列表的collectionview,因此它被命名为listcontroller),而不是recentMessagesNavController 它应该返回navController

以上是关于如何以编程方式使用 Swift 3 在所有视图控制器中显示选项卡栏?的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式返回具有全局功能的根视图控制器?

以编程方式设置初始视图控制器 Swift

Swift 以编程方式导航到另一个视图控制器/场景

如何在 ios swift 中以编程方式将情节提要视图控制器加载到标签栏控制器中?

以编程方式呈现新的 ViewController [Swift 3]

在 Swift 中以编程方式设置根视图控制器