iOS自定义选项卡栏

我刚刚开始iOS开发,只是在atm玩游戏。

我试图将默认的标签栏button变成更自定义的东西。

经过一番四处张望,我发现你可以为每个button创build自定义状态,所以我做了:

UIImage *selectedImage0 = [UIImage imageNamed:@"first.png"]; UIImage *unselectedImage0 = [UIImage imageNamed:@"second.png"]; UITabBar *tabBar = self.tabBarController.tabBar; UITabBarItem *item0 = [tabBar.items objectAtIndex:0]; [item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0]; 

但是,我不能摆脱默认button,它会更改图像,但它不会更改我的整个button。

还有什么我需要做的吗?

 UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; self.tabBarController = [[UITabBarController alloc] init]; self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil]; self.window.rootViewController = self.tabBarController; [self.window makeKeyAndVisible]; UIImage *selectedImage0 = [UIImage imageNamed:@"first.png"]; UIImage *unselectedImage0 = [UIImage imageNamed:@"second.png"]; UITabBar *tabBar = self.tabBarController.tabBar; UITabBarItem *item0 = [tabBar.items objectAtIndex:0]; [item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0]; 

检查以下链接(大多数标签栏控制器的自定义)

  1. 实现一个自定义选项卡栏
  2. 带自定义颜色的Tabbar
  3. 自定义标签栏差异。 背景
  4. 如何保存用户自定义的Tab键顺序 ]
  5. RX -Tabbar控制器

在这里,我创build了一个自定义选项卡栏,图像我是在一个不变的文件。 在这里你可以根据你的方便用“foo.png”replace图像。 这里serivceImg,contactImg等是在.h文件中声明的UIImageView。 此外,不要忘记添加UITabBarControllerDelegate作为您的.h文件中的代理。

 -(void)setUpTabBar { tabBar = [[UITabBarController alloc] init]; Services *firstViewController = [[Services alloc]init]; firstViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1]; UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:firstViewController]; ContactUs *secondViewController = [[ContactUs alloc]init]; secondViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:2]; UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:secondViewController]; Bookings *thirdViewController = [[Bookings alloc]init]; thirdViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3]; UINavigationController *thirdNavController = [[UINavigationController alloc]initWithRootViewController:thirdViewController]; Reward *fourthViewController = [[Reward alloc]init]; fourthViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:4]; UINavigationController *fourthNavController = [[UINavigationController alloc]initWithRootViewController:fourthViewController]; tabBar.viewControllers = [[NSArray alloc] initWithObjects:firstNavController, secondNavController, thirdNavController, fourthNavController, nil]; tabBar.delegate=self; tabBar.selectedIndex=0; [firstNavController release]; [firstViewController release]; [secondNavController release]; [secondViewController release]; [thirdNavController release]; [thirdViewController release]; [fourthNavController release]; [fourthViewController release]; serivceImg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 432, 80, 49)]; serivceImg.image=[UIImage imageNamed:serviceHover]; contactImg=[[UIImageView alloc]initWithFrame:CGRectMake(81, 432,80, 49)]; contactImg.image=[UIImage imageNamed:tabContact]; bookingImg=[[UIImageView alloc]initWithFrame:CGRectMake(162, 432,80, 49)]; bookingImg.image=[UIImage imageNamed:tabBooking]; rewardImg=[[UIImageView alloc]initWithFrame:CGRectMake(243, 432, 80, 49)]; rewardImg.image=[UIImage imageNamed:tabReward]; [tabBar.view addSubview:serivceImg]; [tabBar.view addSubview:contactImg]; [tabBar.view addSubview:bookingImg]; [tabBar.view addSubview:rewardImg]; [[[UIApplication sharedApplication]keyWindow]addSubview:tabBar.view]; [serivceImg release]; [contactImg release]; [bookingImg release]; [rewardImg release]; } - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController1{ if (viewController1 == [tabBar.viewControllers objectAtIndex:0]) { serivceImg.image = [UIImage imageNamed:kserviceHover]; contactImg.image=[UIImage imageNamed:ktabContact]; bookingImg.image=[UIImage imageNamed:ktabBooking]; rewardImg.image=[UIImage imageNamed:ktabReward]; } else if (viewController1 == [tabBar.viewControllers objectAtIndex:1]) { serivceImg.image = [UIImage imageNamed:ktabService]; contactImg.image=[UIImage imageNamed:kcontactHover]; bookingImg.image=[UIImage imageNamed:ktabBooking]; rewardImg.image=[UIImage imageNamed:ktabReward]; } else if (viewController1 == [tabBar.viewControllers objectAtIndex:2]) { serivceImg.image = [UIImage imageNamed:ktabService]; contactImg.image=[UIImage imageNamed:ktabContact]; bookingImg.image=[UIImage imageNamed:kbookingHover]; rewardImg.image=[UIImage imageNamed:ktabReward]; } else if (viewController1 == [tabBar.viewControllers objectAtIndex:3]) { serivceImg.image = [UIImage imageNamed:ktabService]; contactImg.image=[UIImage imageNamed:ktabContact]; bookingImg.image=[UIImage imageNamed:ktabBooking]; rewardImg.image=[UIImage imageNamed:krewardHover]; } } 

试试这个。这可能对你有帮助。