在ios 4中的Tabbar自定义
我正在研究一个iOS应用程序,我需要自定义tabbar是这样的:
Tabbar图像
networkingsearch给了我这个解决scheme:
[self.tabBarItem setFinishedSelectedImage:<#(UIImage *)#> withFinishedUnselectedImage:<#(UIImage *)#>]
但它是iOS5。 有没有可以在iOS4和iOS5上使用的解决scheme?
我们使用下面的代码。你可以定制tabbar
self.tabBarController = [[UITabBarController alloc]init]; firstClassobj.tabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"yourtext", @"yourtext) image:[UIImage imageNamed:@"your Image"] tag:0]; secondClassobj.tabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"yourtext", @"yourtext) image:[UIImage imageNamed:@"your Image"] tag:0]; self.tabBarController .viewControllers = [NSArray arrayWithObjects:firstClassobj,secondClassobj,nil];
//这里是我成功实现的代码…….
//在.h中设置委托UITabBarControllerDelegate,UITabBarDelegate
UINavigationController *navigationController; UITabBarController *tabBarController; @property(strong,nonatomic) UINavigationController *navigationController; @property(strong,nonatomic) UITabBarController *tabBarController;
//在.m中
tabBarController = [[UITabBarController alloc] init]; [tabBarController setDelegate: self];
//用于tabbar背景tabBarController.tabBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@“tabbg.png”]];
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];
/ /标签栏select和未选中的图标
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];