I have a problem with setting up a programmed tab bar. I created a class for a custom tab bar, where in the viewWillAppear method viewWillAppear announced the tab bar itself with the required view:

 override func viewWillAppear(animated: Bool) { let mondayTab = MondayTableViewController() mondayTab.tabBarItem.title = "Monday" mondayTab.tabBarItem.image = UIImage(named: "") let tuesdayTab = TuesdayTableViewController() tuesdayTab.tabBarItem.title = "Tuesday" tuesdayTab.tabBarItem.image = UIImage(named: "") let wednesdayTab = WednesdayTableViewController() wednesdayTab.tabBarItem.title = "Wednesday" wednesdayTab.tabBarItem.image = UIImage(named: "") let thursdayTab = TuesdayTableViewController() thursdayTab.tabBarItem.title = "Thursday" thursdayTab.tabBarItem.image = UIImage(named: "") let fridayTab = TuesdayTableViewController() fridayTab.tabBarItem.title = "Friday" fridayTab.tabBarItem.image = UIImage(named: "") let tabBarController = [mondayTab, tuesdayTab, wednesdayTab, thursdayTab, fridayTab] self.viewControllers = tabBarController } 

In the viewDidLoad function of the same class, I tried to change the boot view in various ways, for example:

 tabBarController?.selectedIndex = 1 

But it did not give the proper effect ...

  • viewdidload occurs before viewdidappear, so you try to select the first tab before it is created - Max Mikheyenko

2 answers 2

The viewDidLoad method viewDidLoad called earlier than viewWillAppear , so when you try to change the index, your tab bar does not have the entire view.

To fix this, either call selectedIndex right at the end in viewWillAppear , or change all the logic so that the selection takes place after creation. The order of the delegate methods in this situation is viewDidLoad-viewWillAppear-viewDidAppear

    Your custom tab bars should be placed in the awakeFromNib() function