开发者

how to modify title and badge icon in UITabBarController

开发者 https://www.devze.com 2023-01-21 23:08 出处:网络
I have the following code: tabBarViewController = [[TabBarViewController alloc] init]; mvc = [[MapViewController alloc] init];

I have the following code:

tabBarViewController = [[TabBarViewController alloc] init];
mvc = [[MapViewController alloc] init];
tvc = [[TableViewController all开发者_Go百科oc] init];
tabBarViewController.viewControllers = [NSArray arrayWithObjects: tvc, mvc, nil];

However, when opening up in the simulator, I don't see any title or badge for these two UITabBar. How can I assign title and badge to these two ?


You must set each child view controller's tabBarItem property:

MapViewController* mvc = [[MapViewController alloc] init];
UIImage* mapIcon = [UIImage imageNamed:@"mapIcon.png"];
mvc.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"Map" image:mapIcon tag:0] autorelease];
mvc.tabBarItem.badgeValue = @"abc";

TableViewController* tvc = [[TableViewController alloc] init];
tvc.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:0] autorelease];
mvc.tabBarItem.badgeValue = @"100";


TabBarViewController has a property to its UITabBar.
The UITabBar has the list of UITabBarItem that have the badge

[tabBarViewController.tabBar.items objectAtIndex:0].badgeValue = @"1";


why not you use this code in tvc, mvc

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization

  self.tabBarItem = [ [ UITabBarItem alloc ]
         initWithTitle:@"YOUR TITLE"
         image:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"IMAGE" ofType:@"png"]]
         tag:1 ];

    }
    return self;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消