以编程方式创建TABBAR

时间:2010-10-13 12:15:18

标签: iphone

我以这种方式以编程方式创建了TAb吧

UITabBarController  *tabBarController = [[UITabBarController alloc] init];


    contacts *vc1 = [[contacts alloc]init];

    vc1.tabBarItem.image=[UIImage imageNamed:@"contacts.png"];

    search* vc2 = [[search alloc] init];

    vc2.tabBarItem.image=[UIImage imageNamed:@"search.png"];

    a1* vc3 = [[a1 alloc] init];

    a2 *vc4 = [[a2 alloc] init];

    a3 *vc5 = [[a3 alloc] init];


    NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2,vc3,vc4,vc5, nil];
    tabBarController.viewControllers = controllers;

    [self.view addSubview:detailNavCont];
    // Add the tab bar controller's current view as a subview of the window
    [self.view addSubview:tabBarController.view];

我想要完成的是,我想将图像分配到标签栏,首先我尝试了前2个标签,显示的是蓝色块而不是图像。 其次,当我们通过界面构建​​器创建标签栏时,有自定义标签栏项,如联系人,搜索,书签,撰写等。

所以,如果我想将联系人或搜索图像分配给我的标签栏项目,看起来像IB中的那个,我该怎么办?

问候

2 个答案:

答案 0 :(得分:15)

尝试这样的事情

tabBars = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];

HomeTabViewController *ptr_homeTab;
ptr_homeTab = [[HomeTabViewController alloc]initWithNibName:@"HomeTabViewController" bundle:nil];
UINavigationController *homeNavBar=[[UINavigationController alloc]initWithRootViewController:ptr_homeTab];
homeNavBar.tabBarItem.title=@"Home";
homeNavBar.tabBarItem.image=[UIImage imageNamed:@"home.png"];
[ptr_homeTab release];

myHospitalviewController=[[MyHospitalViewController alloc]initWithNibName:@"MyHospitalViewController" bundle:nil];
UINavigationController *myHospitalNavBar=[[UINavigationController alloc]initWithRootViewController:myHospitalviewController];
myHospitalNavBar.title=@"My Hospital";
myHospitalNavBar.tabBarItem.image=[UIImage imageNamed:@"myhospital.png"];
[myHospitalviewController release];

viewController = [[TreatMentiViewController alloc]initWithNibName:@"TreatMentiViewController" bundle:nil];
UINavigationController *hospitalNavBar=[[UINavigationController alloc]initWithRootViewController:viewController];
hospitalNavBar.tabBarItem.title=@"Hospital";
hospitalNavBar.tabBarItem.image=[UIImage imageNamed:@"hospital.png"];
[viewController release];

PersonalMedicineViewController *ptr_PersonalMedicine = [[PersonalMedicineViewController alloc] initWithNibName:@"PersonalMedicineViewController" bundle:nil];
UINavigationController *managerNavBar=[[UINavigationController alloc]initWithRootViewController:ptr_PersonalMedicine];
managerNavBar.tabBarItem.title=@"Manager";
managerNavBar.tabBarItem.image=[UIImage imageNamed:@"manager.png"];
[ptr_PersonalMedicine release];

[localViewControllersArray addObject:homeNavBar];
[localViewControllersArray addObject:hospitalNavBar];
[localViewControllersArray addObject:myHospitalNavBar];
[localViewControllersArray addObject:managerNavBar];

[homeNavBar release];
[hospitalNavBar release];
[myHospitalNavBar release];
[managerNavBar release];

tabBars.viewControllers = localViewControllersArray;
tabBars.view.autoresizingMask==(UIViewAutoresizingFlexibleHeight);

[localViewControllersArray release];

[window addSubview:tabBars.view];

答案 1 :(得分:3)

您的图片需要有适当的面具。你应该保存为png。