【问题标题】:Create iOS Tab bar dynamically动态创建 iOS 标签栏
【发布时间】:2018-08-05 17:22:19
【问题描述】:

我想根据服务器响应动态创建标签栏。标签计数每次都会改变。以及如何设计视图所有视图都具有相同的设计只有数据会改变

【问题讨论】:

  • 到目前为止你尝试过什么?
  • 显示你尝试过的代码
  • 我已经尝试过静态标签。我想建立动态标签。所以我需要构建动态标签的逻辑。我是 ios 开发新手。
  • @Dhinakaran 你在动态标签上找到解决方案了吗

标签: ios swift uitabbarcontroller uitabbar tabbar


【解决方案1】:

假设你有 vc1,vc2,vc3

self.tabBarController.viewControllers = [vc1,vc2,vc3]

【讨论】:

    【解决方案2】:

    为标签栏控制器创建一个单独的类。在那里创建一个必须返回标签栏控制器的方法。

    在该方法中,传递一个数组,在每个索引上,它将包含标签栏名称、图像、选定图像、视图控制器。

    【讨论】:

      【解决方案3】:

      在设置视图控制器之前,您需要调用服务器。如果这是您的第一个视图,您将需要在 viewdidload 中执行此操作,并且可能设置一个活动指示器。如果不是,你需要有某种加载屏幕/在你的 tabBar 之前拨打电话。

      【讨论】:

        【解决方案4】:

        将此方法放在实用程序类中:

        class func setupTabBarController(tabbarController: UITabBarController) {
        
        
        let hOmeVC = HomePageViewController()
        let skriblBoxVC = MySkribViewController()
        let searchVC = SearchViewController()
        
        tabbarController.tabBar.isExclusiveTouch = true
        tabbarController.view.backgroundColor = UIColor.white
        // Creating navigation Controller and putting them in tabBarController because without it we will not be able to push viewController
        
        let homeNavigationController = UINavigationController()
        let skriblBoxNavigationController = UINavigationController()
        let searchNavigationController = UINavigationController()
        
        
        
        tabbarController.viewControllers = []
        tabbarController.tabBar.isTranslucent = false
        
        tabbarController.viewControllers = [homeNavigationController, skriblBoxNavigationController, searchNavigationController]
        
        tabbarController.selectedIndex = 0
        
        
        
        
        
        
        
        
        tabbarController.tabBar.items![0].image = #imageLiteral(resourceName: "tab_home")
        tabbarController.tabBar.items![1].image = #imageLiteral(resourceName: "tab_box")
        
        tabbarController.tabBar.items![SBModules.SEARCH.rawValue].image = #imageLiteral(resourceName: "tab_search")
        
        tabbarController.tabBar.items![2].image = #imageLiteral(resourceName: "tab_cart")
        
        
        
        tabbarController.tabBar.items![0].selectedImage = #imageLiteral(resourceName: "tab_home_selected")
        
        tabbarController.tabBar.items![1].selectedImage = #imageLiteral(resourceName: "tab_box_selected")
        
        tabbarController.tabBar.items![2].selectedImage = #imageLiteral(resourceName: "tab_search_selected")
        
        tabbarController.tabBar.barTintColor = UIColor.white
        
        tabbarController.tabBar.tintColor = UIColor.tabBarBadgeColorWithAlpha()
        
        tabbarController.tabBar.itemPositioning = .automatic
        
        tabbarController.tabBar.itemSpacing = 2.0
        
        tabbarController.tabBarItem.title = nil
        
        if let items = tabbarController.tabBar.items {
            for item in items {
                item.title = ""
                item.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
            }
        }
        
        }
        

        并将这个方法归类为 viewController 喜欢

        let tabbarController = UITabBarController()
        
         func loadHomePage() {
        Utilities.setupTabBarController(tabbarController:      self.tabbarController)
        self.updateBadgeValueToCart()
        self.window?.rootViewController = self.tabbarController
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-07
          • 1970-01-01
          • 1970-01-01
          • 2019-01-04
          • 2020-01-24
          • 2021-04-22
          相关资源
          最近更新 更多