Adding TabBar element icons by code, not to interface builder?

I am making an application with a Dapp application, but I cannot correctly add the icons in the tab bar. So, is there a way to add icons to the tab bar encoding it? Like something in AppDelegate, without using Interface Builder. Thanks!

+6
source share
2 answers

UITabBarController displays all the information needed to create a tab bar item for each view controller by checking the view controller itself.

All you have to do is assign an array of view controllers using -setViewControllers:animated:

After adding the view controller to the tab bar controller, the tab bar controller will check the tabBarItem property of the tabBarItem controller. This tab bar item will automatically be inserted into the tab bar of the tab bar panel. You can initialize the tab bar in each view controller programmatically. It looks something like this ...

 UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Title" image:[UIImage imageNamed:@"someImage"] tag:1]; 

or if you want to use one of the elements of the system ...

 UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithSystemItem:UITabBarSystemItemFeatured tag:1]; 

I suggest you take a look at UITabBarController , UITabBarItem and UIViewController and read about the corresponding properties. Documents are filled with irreplaceable information.

+6
source

Yes, if you have your 30x30.png files in your resources, this should just be adding code, like what follows. You would put this in your init controller method of the view associated with the index of the tab bar.

 //get the tab bar item UITabBarItem *tbi = [self tabBarItem]; //Give it a label [tbi setTitle:@"Item One"]; //create a UIImage from a file UIImage *i = [UIImage imageNamed:@"MyItem.png"]; //put that image on the tab bar item [tbi setImage:i]; 
+2
source

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


All Articles