Iphone UITabBarItem image not shown

My question is how I could add the icon of a childViewController of an UITabBarController. My code is:

UITabBarController *tabBar=[[UITabBarController alloc]init];
MyUIViewController *mc=[[MyUIViewController alloc]init];
[tabBar addChildViewController:mc];
[self.navigationController pushViewController:tabBar animated:YES];

Thanks in advance.

Upvotes: 0

Views: 1235

Answers (3)

MobileSandbox1
MobileSandbox1

Reputation: 1

I just discovered that the desktop is case insensitive, however the device requires the code to have the same alpha-numberic case (upper/lower). Make sure the name of the file is the same ... down to the case (upper/lower)!

Example:

The file name on disk of "first.png" had better be represented as --> self.tabBarItem.image = [UIImage imageNamed:@"first"];

NOT -- NOT --- NOT

self.tabBarItem.image = [UIImage imageNamed:@"First"];

The capital 'F' kicked my arse for 3 hours today! I lost 3 hours of my life today because of this oversight. Hope this helps someone else.

Upvotes: 0

I have solved this by adding an attribute and property in MyUIViewController:

@interface MyUIViewController{
    UITabBarItem *tabBarItem;
}
@property (nonatomic, retain) UITabBarItem *tabBarItem;

and then initialize it with an image:

UITabBarController *tabBar=[[UITabBarController alloc]init];
MyUIViewController *mc=[[MyUIViewController alloc]init];
mc.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"MyTitle" image:[UIImage imageNamed:@"myImage.png"] tag:0];
[tabBar addChildViewController:mc];
[self.navigationController pushViewController:tabBar animated:YES];

Upvotes: 0

Krrish
Krrish

Reputation: 2256

Please try this

UITabBarController *tabController = [[UITabBarController alloc] init];
SomeViewController *viewController = [[SomeViewController alloc] initWithNibName:@"SomeViewController" bundle:nil];
[tabController setViewControllers:[NSArray arrayWithObject:viewController]];//Setting child viewController Array.
UITabBarItem *item = (UITabBarItem *)[tabController.tabBar.items objectAtIndex:0]; //for first view
[item setImage:[UIImage imageNamed:@"someImage.png"]]; //Image should be 23px X 23px ,I think so.

Upvotes: 1

Related Questions