0

我想更改 TabBarItem 的默认效果,在选择和取消选择项目后获取。我使用这种方法解决了ios5及更高版本的这个问题

    [mySchedule setFinishedSelectedImage:[UIImage imageNamed:@"myschedule.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"aboutus.png"]]; 

但对于版本 4 及以下版本,它会出现问题。请告诉我如何解决版本 4 及以下版本的这个问题?

4

2 回答 2

1

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

}

}

希望这能解决您的问题。

于 2012-05-01T10:03:30.050 回答
0

从图像名称中删除 .png。

于 2012-04-30T11:02:28.683 回答