我想实现标签栏,这是实现它的最佳且不那么繁忙的方式吗?
答案 0 :(得分:2)
我不知道你想要这样或不想,但我在所有项目中都这样做..
- (void)viewDidLoad
{
[self setmenu];
}
-(void)setmenu:(id)sender
{
UIImage *tabBackground = [[UIImage imageNamed:@"tabbg.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UITabBar appearance] setBackgroundImage:tabBackground];
UIColor *titleHighlightedColor = [UIColor whiteColor];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
titleHighlightedColor, UITextAttributeTextColor,
nil] forState:UIControlStateSelected];
[[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeFont:[UIFont systemFontOfSize:12], UITextAttributeTextColor:[UIColor colorWithRed:59.0/255.0f green:59.0/255.0f blue:59.0/255.0f alpha:1.0f]} forState:UIControlStateNormal];
UITabBarItem *tabBarItem1 = [theTabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [theTabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [theTabBar.items objectAtIndex:2];
tabBarItem1.title =[appDelegate.dict objectForKey:@"One"];
tabBarItem2.title = [appDelegate.dict objectForKey:@"Two"];
tabBarItem3.title = [appDelegate.dict objectForKey:@"Three"];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"one_active.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"one.png"]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"two_active.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"two.png"]];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"three_active.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"three.png"]];
[theTabBar setSelectedItem:[theTabBar.items objectAtIndex:0]];
UIImage *tabBackground1 = [[UIImage imageNamed:@"tabbg_active.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UITabBar appearance] setSelectionIndicatorImage:tabBackground1];
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if([item.title isEqual: [appDelegate.dict objectForKey:@"One"]])
{
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
One *mvc = [sb instantiateViewControllerWithIdentifier:@"One"];
[self.navigationController pushViewController:mvc animated:YES];
}
else if([item.title isEqual: [appDelegate.dict objectForKey:@"Two"]])
{
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
Two *mvc = [sb instantiateViewControllerWithIdentifier:@"Two"];
[self.navigationController pushViewController:mvc animated:YES];
}
else if([item.title isEqual: [appDelegate.dict objectForKey:@"Three"]])
{
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
Three *mvc = [sb instantiateViewControllerWithIdentifier:@"Three"];
[self.navigationController pushViewController:mvc animated:YES];
}
}