如何更改UITabBarItem默认选择在iPhone中取消选择效果?

时间:2012-04-30 10:59:32

标签: iphone uitabbar uitabbaritem

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

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

但是对于版本4及以下版本,它会产生问题。请告诉我如何解决版本4及以下版本的问题?

2 个答案:

答案 0 :(得分:1)

这里我创建了一个自定义标签栏,它有四个按钮。按钮由图像组成,我将其放在一个常量文件中。这里的serivceImg,contactImg等是在.h文件中声明的UIImageView。另外,不要忘记在.h文件中添加UITabBarControllerDelegate作为委托。

-(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];

}

}

希望这能解决你的问题。

答案 1 :(得分:0)

从图片名称中删除.png。