通过程序更改选项卡栏项目标题和颜色



我在AppDelegate中实现了一个选项卡栏控制器,但它只是选项卡栏中的空方块。我希望可以更改它们的标题和图像,我还想知道如何不仅使用我添加的自定义图像,还使用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;
}

您必须创建自己的UITabBarItem

在你的appdelegate中,你可以做一些类似的事情:

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

回归自我;

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

最新更新