Create iOS Tab bar dynamically

2019-06-14 20:37发布

I want to create tab bar dynamically based on server response. Tab count will change every time. And how to design the views all view have the same design only data will change

4条回答
祖国的老花朵
2楼-- · 2019-06-14 21:02

suppose you have vc1,vc2,vc3

self.tabBarController.viewControllers = [vc1,vc2,vc3]
查看更多
再贱就再见
3楼-- · 2019-06-14 21:03

You'll need to make your server call BEFORE you set up the view controller. If it is your first view, you will need to do this in viewdidload and perhaps set up an activity indicator. If not you need to have some sort of loading screen / make your call in the immediately prior to your tabBar.

查看更多
Rolldiameter
4楼-- · 2019-06-14 21:10

Put This Method in Utilities Class :

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

}

and class this method form viewController Like

let tabbarController = UITabBarController()

 func loadHomePage() {
Utilities.setupTabBarController(tabbarController:      self.tabbarController)
self.updateBadgeValueToCart()
self.window?.rootViewController = self.tabbarController
}
查看更多
forever°为你锁心
5楼-- · 2019-06-14 21:12

Create a separate class for tab bar controller. Create a method there which must return tab bar controller.

In the method, pass an array, on each index it will contains the tab bar name, image, selected image, view controller.

查看更多
登录 后发表回答