iOS custom tabbar

2019-01-06 11:47发布

问题:

I just started with iOS development and am just playing around atm.

I'm trying to transform the default tabbar buttons into something more custom.

After some looking around I found you can create custom states for every button, so I did:

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

However, I can't get rid of the default button, it changes the image, but it doesn't change my entire button.

Is there something else I need to do?

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:

Check the following Links(Most of the customization of the tab-bar controller)

  1. Implement a Custom Tab Bar
  2. Tabbar with custom colors
  3. Custom Tabbar diff. background
  4. How to Save User Customized Tab Order]
  5. RX -Tabbar controller


回答2:

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.