Reputation: 1165
I've been trying to change the title of my tab with the following code,
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = @"City Search";
self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1];
}
delegate = (AWSAppDelegate *)[[UIApplication sharedApplication] delegate];
return self;
}
That gives me a iOS native search icon and test. but what I want to do is add my own title, and I tried doing the following,
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.tabBarItem = [[UITabBarItem alloc] init]
self.tabBarItem.image = myImage;
self.tabBarItem.title = @"FooBar";
}
delegate = (AWSAppDelegate *)[[UIApplication sharedApplication] delegate];
return self;
}
However this just gives me a blank tab without any text, everything else works ok. can you please help?
I'm using iOS 6
Upvotes: 0
Views: 1235
Reputation: 1165
Following actually worked :) However I've noticed that if the image file does not exist, this won't work...
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
delegate = (AWSAppDelegate *)[[UIApplication sharedApplication] delegate];
if (self) {
self.title = @"City Search";
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"about_tap_icon.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"about_tap_icon.png"]];
[self.tabBarItem setBadgeValue:@"about"];
[self.tabBarItem setTitle:@"hello"];
}
return self;
}
Upvotes: 0
Reputation: 928
The UITabBarItem for this UIViewController would've already been created and added - so there's no need to allocate a new one.
Just modify the current UITabBarItem.
You can also set the selected image like so:
-(void)awakeFromNib
{
self.title = @"City Search";
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"citySearch_selected"]withFinishedUnselectedImage:[UIImage imageNamed:@"citySearch_unselected"]];
}
Upvotes: 0
Reputation: 47049
use following code :
myTabItemContainViewController *addVC = [[myTabItemContainViewController alloc] init]; /// initialize object of your viewController
self.tabBarItem = [[UITabBarItem alloc] init]
self.tabBarItem.image = myImage;
self.tabBarItem.title = @"FooBar";
[addVC setTabBarItem: self.tabBarItem ];
You need to add your tabBarItem to your specific viewController.
Upvotes: 2
Reputation: 169
Here is a simple solution, your image must be black and white and in proper size 30x30
self.title=NSLocalizedString(@"foobar", nil);
self.tabBarItem.image=[UIImage imageNamed:@"image.png"];
Upvotes: 2