4

我刚开始使用 iOS 开发,只是在玩 atm。

我正在尝试将默认的标签栏按钮转换为更自定义的东西。

环顾四周后,我发现您可以为每个按钮创建自定义状态,所以我这样做了:

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

但是,我无法摆脱默认按钮,它会更改图像,但不会更改我的整个按钮。

还有什么我需要做的吗?

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

2 回答 2

11

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

  1. 实现自定义标签栏
  2. 带有自定义颜色的标签栏
  3. 自定义标签栏差异。背景
  4. 如何保存用户自定义的标签顺序]
  5. RX -Tabbar 控制器
于 2012-04-27T06:13:32.763 回答
4

Here i've created a custom tab bar and images i took are in a constant file. Here you can replace the image with "foo.png" as per your convenience. Here the serivceImg, contactImg etc are UIImageView which is declared in .h file. Also, don't forget to add UITabBarControllerDelegate as a delegate in your .h file.

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

}

}

Try this.This might help you.

于 2012-04-27T06:30:50.917 回答