Xcode Add tab to tab dynamically

I read various answers about adding tabbed icons in both the application delegate and the interface builder. I want to add them to my firtsviewcontroller because I wanted to read the name of the icon image with a plist . It is proposed to do the following:

 NSArray *viewControllerArray = nil; viewController1 = <View Init Code> viewController2 = <View Init Code> viewController3 = <View Init Code> 1stNavController = [[UINavigationController alloc] initWithRootViewController:viewController1]; UIImage *img = [UIImage imageNamed:@"tab_home"]; [1stNavController .tabBarItem initWithTitle:@"Home" image:img tag:1]; 

But I do not like to do it this way, because I think it is not clean. I would like to do something similar to the following, but I do not know why it does not work:

 [[self.tabBarController.tabBar.items objectAtIndex:2] setIcon:[UIImage imageNamed:....]]; 

Any solution?

+4
source share
1 answer

How about something like:

 NSArray *viewControllers = self.tabBarController.viewControllers; ((UIViewController*)viewControllers[0]).tabBarItem.image = [UIImage imageNamed:...]; ((UIViewController*)viewControllers[1]).tabBarItem.image = [UIImage imageNamed:...]; ((UIViewController*)viewControllers[2]).tabBarItem.image = [UIImage imageNamed:...]; ((UIViewController*)viewControllers[3]).tabBarItem.image = [UIImage imageNamed:...]; ((UIViewController*)viewControllers[4]).tabBarItem.image = [UIImage imageNamed:...]; 
0
source

Source: https://habr.com/ru/post/1436219/


All Articles