ios 4中的tabbar自定义 [英] Tabbar customization in ios 4
问题描述
我正在开发iOS应用程序,我需要将tabbar自定义如下:
I'm working on an iOS application and I need to customize the tabbar to be like this:
网络搜索给了我这个解决方案:
A web search gave me this solution:
[self.tabBarItem setFinishedSelectedImage:<#(UIImage *)#> withFinishedUnselectedImage:<#(UIImage *)#>]
但它适用于iOS5。有没有适用于iOS4和iOS5的解决方案?
But it is for iOS5. Is there a solution that works in both iOS4 and iOS5?
推荐答案
//这是我成功实施的代码... ....
//here is the code that i implement successfully.......
//在.h中设置委托UITabBarControllerDelegate,UITabBarDelegate
//in .h set delegate UITabBarControllerDelegate,UITabBarDelegate
UINavigationController *navigationController;
UITabBarController *tabBarController;
@property(strong,nonatomic) UINavigationController *navigationController;
@property(strong,nonatomic) UITabBarController *tabBarController;
// in .m
tabBarController = [[UITabBarController alloc] init];
[tabBarController setDelegate: self];
//用于标签栏背景
tabBarController.tabBar.tintColor = [UIColor colorWithPatternImage:[UIImage] imageNamed:@tabbg.png]];
//for tabbar background tabBarController.tabBar.tintColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbg.png"]];
NSMutableArray * localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
Demo1 *home = [[Login alloc] initWithNibName:@"Login" bundle:nil];
localNavController = [[UINavigationController alloc] initWithRootViewController:home];
localNavController.tabBarItem.title=@"demo1";
[localViewControllersArray addObject:localNavController];
Demo2 *puck=[[Demo2 alloc]initWithNibName:@"Demo2" bundle:nil];
localNavController = [[UINavigationController alloc] initWithRootViewController:puck];
localNavController.tabBarItem.title=@"demo2";
[localViewControllersArray addObject:localNavController];
Demo3 *photo=[[Demo3 alloc]initWithNibName:@"Demo3" bundle:nil];
localNavController = [[UINavigationController alloc] initWithRootViewController:photo];
localNavController.tabBarItem.title=@"demo3";
[localViewControllersArray addObject:localNavController];
Demo4 *more=[[Demo4 alloc]initWithNibName:@"Demo4" bundle:nil];
localNavController = [[UINavigationController alloc] initWithRootViewController:more];
localNavController.tabBarItem.title=@"demo4";
[localViewControllersArray addObject:localNavController];
[[UITabBarItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor grayColor], UITextAttributeTextColor,
[UIColor grayColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Helvetica" size:0.0], UITextAttributeFont,
nil]
forState:UIControlStateNormal];
//标签栏选中和未选中的图标
//tab bar selected and unselected icons
UITabBarItem *tabBarItem1 = [[self.tabBarController.tabBar items] objectAtIndex:0];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"puckCentralA.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"puckCentral.png"]];
UITabBarItem *tabBarItem2 = [[self.tabBarController.tabBar items] objectAtIndex:1];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"puckDisplayA.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"puckDisplay.png"]];
UITabBarItem *tabBarItem3 = [[self.tabBarController.tabBar items] objectAtIndex:2];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"photoBoothA.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"photoBooth.png"]];
UITabBarItem *tabBarItem4 = [[self.tabBarController.tabBar items] objectAtIndex:3];
[tabBarItem4 setFinishedSelectedImage:[UIImage imageNamed:@"moreA.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"more.png"]];
[self.window addSubview:tabBarController.view];
[self.window addSubview:tabBarController.view];
这篇关于ios 4中的tabbar自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!