Program UITabBarItem icons with StoryBoards?

I want to use UITabBarSystemItem for the icon of one of my tabBarItem, but I use storyboards. I do not know where to install it. If I set it to view viewDidLoad, it will not change until you click the button in the tabBar. Before that, is it just blue? square.

And as far as I know, you cannot use UITabBarSystemItems in IB inspector.

UPDATE:

Well, first of all, I'm an idiot. You can fully select the icon in the IB Inspector.

Tab bar item → ID → Select the icon.

But the question remains how to do this programmatically. Rather, when / where?

+4
source share
4 answers

When using storyboards, the view controller initializes the use of the initWithCoder constructor so that you can override this function and set the system element icon there, for example:

- (id)initWithCoder:(NSCoder*)aDecoder { if(self = [super initWithCoder:aDecoder]) { self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1]; } return self; } 

Naturally, you can change the value of a system element to any of the supported values. Apple lists them here

+11
source

Check this code for your problem: its working in my application.

 - (NSArray *) initializeViewControllers { 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]; 2ndNavController = [[UINavigationController alloc] initWithRootViewController:viewController2]; img = [UIImage imageNamed:@"tab_timeDrop"]; [2ndNavController .tabBarItem initWithTitle:@"Time Entry" image:img tag:2]; 3rdNavController = [[UINavigationController alloc] initWithRootViewController:viewController3]; img = [UIImage imageNamed:@"tab_invoiceSummary"]; [3rdNavController.tabBarItem initWithTitle:@"Invoice Summary" image:img tag:3]; viewControllerArray = [NSArray arrayWithObjects:1stNavController,2ndEntryNavController,3rdReportNavController, nil]; return viewControllerArray; } 

This code returns View Controllers with images for their respective tabs. Here I used the navigation controller inside the panel controller. You can also use the view controller instead of the navigation controller.

Just add this code and initialize your tab controller as follows inside the appdidfinishlaunching method:

  tabbarController = [[UITabBarController alloc] init]; _tabbarController.viewControllers = [self initializeViewControllers]; self.window.rootViewController = tabbarController; 

Hope this works.

Please reply.

+7
source

Well, it is very simple!

For where -> create a custom TabBarController (e.g. WBTabBarController). Explicitly install it in your StoryBoard.

For how -> just overwrite viewDidLoad from WBTabBarController and set your images for TabBarItems. In the following example, this was done using NIKFontAwesomeIconFactory:

 #import "WBTabBarController.h" #import "NIKFontAwesomeIcon.h" #import "NIKFontAwesomeIconFactory.h" #import "NIKFontAwesomeIconFactory+iOS.h" @interface WBTabBarController () @end @implementation WBTabBarController - (void)viewDidLoad { [super viewDidLoad]; NIKFontAwesomeIconFactory *factory = [NIKFontAwesomeIconFactory tabBarItemIconFactory]; UITabBarItem *item0 = self.tabBar.items[0]; item0.image = [factory createImageForIcon:NIKFontAwesomeIconApple]; UITabBarItem *item1 = self.tabBar.items[1]; item1.image = [factory createImageForIcon:NIKFontAwesomeIconStackOverflow]; } @end 
+3
source

Assuming the start-up scene of your storyboard is a tab bar view controller, you can access it in applicationDidFinishLaunching, this is the rootViewController window application delegate. I do not know if you can swap system elements if the panel element is already present, but you can set the image and title.

+2
source

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


All Articles