在这里,我创建了一个具有四个按钮的自定义标签栏。按钮由图像组成,我将其放入一个常量文件中。这里的 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];
}
}
希望这能解决您的问题。