Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set iphone tab bar icon by custom images using story board

I am newbie to iphone development,I need to put a custom image icon in the tab bar .But it showing only the default colour(Black&blue) .Help me to overcome from this bug friends...?

like image 604
MohanRaj S Avatar asked Dec 21 '12 10:12

MohanRaj S


People also ask

What is tab bar in iOS?

A TabBar is a control that is used to display and configure one or more bar button items in a tab bar for selecting between different subtasks, views, or modes in the iOS application. The TabBar is used in association with the TabBarController to present the list of tabs in the application.


3 Answers

add the Images in application,

and See attached Image

enter image description here

like image 83
junaidsidhu Avatar answered Sep 26 '22 21:09

junaidsidhu


Use this code in viewDidLoad

UIImage *selectedImage0 = [UIImage imageNamed:@"selected.png"];
UIImage *unselectedImage0 = [UIImage imageNamed:@"unselected.png"];

UITabBarItem *item0 = [tabBar.items objectAtIndex:0];

[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
like image 6
CRDave Avatar answered Sep 22 '22 21:09

CRDave


Use below code in AppDelegate

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary  *)launchOptions
{


UIViewController *viewController1 = [[ViewFirstViewController alloc] init];

UIViewController *viewController2 = [[ViewSecondViewController alloc] init];
UIViewController *viewController3 = [[ViewThirdViewController alloc] init];
UIViewController *viewController4 = [[ViewFourthViewController alloc] init];

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2,viewController3,viewController4, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];


imgTab = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,320.0,50)];
self.tabBarController.tabBar.tag=10;
[self.tabBarController.tabBar addSubview:imgTab];
self.tabBarController.delegate = self;

imghome=[[UIImageView alloc]initWithFrame:CGRectMake(0.0,00.0,80.0,50.0)];
imghome.image=[UIImage imageNamed:@"dressup_active.png"];
[imgTab addSubview:imghome];

imgQuiz=[[UIImageView alloc]initWithFrame:CGRectMake(80.0,00.0,81.0,50.0)];
imgQuiz.image=[UIImage imageNamed:@"x-mas_tree.png"];
[imgTab addSubview:imgQuiz];

imgtTW=[[UIImageView alloc]initWithFrame:CGRectMake(161.0,00.0,80.0,50.0)];
imgtTW.image=[UIImage imageNamed:@"greetings.png"];
[imgTab addSubview:imgtTW];

imgGuest=[[UIImageView alloc]initWithFrame:CGRectMake(241.0,00.0,80.0,50.0)];
imgGuest.image=[UIImage imageNamed:@"quotes_tab.png"];
[imgTab addSubview:imgGuest];

}

Tabbar Controller delegate method

- (void)tabBarController:(UITabBarController *)tabBarControllers didSelectViewController:(UIViewController *)viewController
{

NSLog(@"%i",tabBarControllers.selectedIndex);
if (tabBarControllers.selectedIndex == 0)
{

    imghome.image=[UIImage imageNamed:@"dressup_active.png"];
    imgQuiz.image=[UIImage imageNamed:@"x-mas_tree.png"];
    imgtTW.image=[UIImage imageNamed:@"greetings.png"];
    imgGuest.image=[UIImage imageNamed:@"quotes_tab.png"];

}
else if (tabBarControllers.selectedIndex == 1)
{

    imghome.image=[UIImage imageNamed:@"dressup.png"];
    imgQuiz.image=[UIImage imageNamed:@"x-mas_tree_active.png"];
    imgtTW.image=[UIImage imageNamed:@"greetings.png"];
    imgGuest.image=[UIImage imageNamed:@"quotes_tab.png"];

}
else if (tabBarControllers.selectedIndex == 2)
{

    imghome.image=[UIImage imageNamed:@"dressup.png"];
    imgQuiz.image=[UIImage imageNamed:@"x-mas_tree.png"];
    imgtTW.image=[UIImage imageNamed:@"greetings_active.png"];
    imgGuest.image=[UIImage imageNamed:@"quotes_tab.png"];

}
else if (tabBarControllers.selectedIndex == 3)
{

    imghome.image=[UIImage imageNamed:@"dressup.png"];
    imgQuiz.image=[UIImage imageNamed:@"x-mas_tree.png"];
    imgtTW.image=[UIImage imageNamed:@"greetings.png"];
    imgGuest.image=[UIImage imageNamed:@"quotes_active.png"];

}



}
like image 4
thavasidurai Avatar answered Sep 25 '22 21:09

thavasidurai