以编程方式更改选项卡栏项目标题和颜色

时间:2012-03-25 07:24:16

标签: iphone objective-c uitabbarcontroller

我在App Delegate中实现了一个标签栏控制器,但它只是标签栏中的空白方块。我希望能够改变它们的标题和图像,我也想知道如何使用我添加的自定义图像,但是在Xcode中实现“默认”图像(“计算器”图像,“搜索”图像)。

如果您在xib中有标签栏,则可以在标签栏项目中看到它 - >属性检查器 - >标识符,然后有一个列表,如果您不想使用自定义图像。所以有我的appDelegate.m代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after app launch


    UIViewController *banksList = [[FailedBanksListViewController alloc] initWithNibName:@"FailedBanksListViewController" bundle:nil];
    UINavigationController *listNavigationController = [[UINavigationController alloc] initWithRootViewController:banksList];

    UIViewController *first = [[BIDViewController alloc] initWithNibName:@"BIDViewController" bundle:nil];    
    UIViewController *second = [[BIDDailyCount alloc] initWithNibName:@"BIDDailyCount" bundle:nil];

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:first,second,listNavigationController, nil];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];    


    return YES;
}

1 个答案:

答案 0 :(得分:2)

你必须创建自己的UITabBarItem

在appdelegate中,您可以执行以下操作:

UIViewController *banksList = [[FailedBanksListViewController alloc] initWithNibName:@"FailedBanksListViewController" bundle:nil];
banksList.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:0];

返回自我;

将其移动到每个选项卡的控制器中的自定义初始化程序可能是个好主意。