动态创建iOS标签栏

时间:2018-02-26 09:40:10

标签: ios swift uitabbarcontroller uitabbar tabbar

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

4 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

假设你有vc1,vc2,vc3

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

答案 2 :(得分:1)

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

答案 3 :(得分:1)

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

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
}