//构建自定义TabBar
- (void)initCustomTabBar{
QFAmountC *amountC = [QFAmountC new];
QFTradeListC *tradeListC = [QFTradeListC new];
QFMerchantC *merchantC = [QFMerchantC new];
QFExpandC *expandC = [QFExpandC new];
UINavigationController *amountNav = [[[UINavigationController alloc]initWithRootViewControll
er:amountC]autorelease];
UINavigationController *tradeListNav = [[[UINavigationController alloc]initWithRootViewControll
er:tradeListC]autorelease];
UINavigationController *merchantNav = [[[UINavigationController alloc]initWithRootViewControll
er:merchantC]autorelease];
UINavigationController *expandNav = [[[UINavigationController alloc]initWithRootViewControll
er:expandC]autorelease];
tabBarController.viewControllers = [NSArray arrayWithObjects:amountNav,tradeListNav,merchantNav,expandNav,nil];
tabBarController.tabBarImagesArray = [NSMutableArray arrayWithObjects:
[UIImage imageNamed:@"tab_card@2x"],
[UIImage imageNamed:@"tab_trade@2x"],
[UIImage imageNamed:@"tab_Profile@2x"],
[UIImage imageNamed:@"tab_apps@2x"],
nil];
tabBarController.tabBarSelectedStateImage
sArray = [NSMutableArray arrayWithObjects:
[UIImage imageNamed:@"tab_card_sel@2x"],
[UIImage imageNamed:@"tab_trade_sel@2x"],
[UIImage imageNamed:@"tab_Profile_sel@2x"],
[UIImage imageNamed:@"tab_apps_sel@2x"],
nil];
[tabBarController initCustomizeTabBar];
}
//初始化自定义TabBar
- (void)initCustomizeTabBar{
[self initCustomizeTabBarImage
s];
if (tabBarButtonsArray) {
[self initCustomizeTabBarTitle
s];
}
[self didSelectTab:[tabBarButtonsArray objectAtIndex:0]];
}
//自定义的image
- (void)initCustomizeTabBarImage
s{
tabBarButtonsArray = [[NSMutableArray alloc] initWithCapacity:0];
int tabItemsCount = [self.viewControllers count];
if (tabItemsCount!= [tabBarImagesArray count] || tabItemsCount != [tabBarSelectedStateImage
sArray count]) {
NSLog(@"tabItemsCount!= [tabBarImagesArray count] || tabItemsCount != [tabBarSelectedStateImage
sArray count]");
return;
}
float tabWidth = self.tabBar.frame.size.width/tabItemsCount;
float tabHeight = self.tabBar.frame.size.height;
float tabXCenter=tabWidth/2;
float tabYCenter = self.tabBar.frame.size.height/2;
for (int i=0;i < [self.viewControllers count]; i++) {
UIButton *tabBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
tabBarButton.tag = i;
[tabBarButton addTarget:self action:@selector(didSelectTab:) forControlEvents:UIControlEventTouchUpIns
ide];
UIImage *buttonImage = [tabBarImagesArray objectAtIndex:i];
tabBarButton.autoresizingMask = UIViewAutoresizingFlexib
leRightMargin | UIViewAutoresizingFlexib
leLeftMargin | UIViewAutoresizingFlexib
leBottomMargin | UIViewAutoresizingFlexib
leTopMargin;
tabBarButton.frame = CGRectMake(0.0, 0.0, tabWidth,tabHeight);
[tabBarButton setImage:buttonImage forState:UIControlStateNormal];
[tabBarButton setImage:buttonImage forState:UIControlStateSelected];
[tabBarButton setImage:buttonImage forState:UIControlStateHighlighte
d];
[tabBarButton setImage:buttonImage forState:UIControlStateSelected|UIControlStateHighlighte
d];
tabBarButton.imageView.image = buttonImage;
tabBarButton.selected = YES;
tabBarButton.center = CGPointMake(tabXCenter, tabYCenter);
tabXCenter += tabWidth;
[tabBarButtonsArray addObject:tabBarButton];
[self.tabBar addSubview:tabBarButton];
}
}
//自定义名称
-(void)initCustomizeTabBarTitle
s{
tabBarLabelsArray = [[NSMutableArray alloc] initWithCapacity:0];
int tabItemsCount = [self.viewControllers count];
if (tabItemsCount!= [tabBarImagesArray count] || tabItemsCount != [tabBarSelectedStateImage
sArray count]) {
NSLog(@"tabItemsCount!= [tabBarImagesArray count] || tabItemsCount != [tabBarSelectedStateImage
sArray count]");
return;
}
//选择tab
- (void)didSelectTab:(id)sender
{
UIButton *selectedButton = (UIButton *)sender;
if (self.selectedIndex == selectedButton.tag) {
[self postNotification:selectedButton.tag];
}
self.selectedIndex = selectedButton.tag;
[self didselectTabAtIndex:selectedButton.tag];
}
//按索引切换
- (void)didselectTabAtIndex:(NSUInteger)selectedTabIndex
{
self.selectedIndex = selectedTabIndex;
for (int i=0; i<[tabBarButtonsArray count]; i++) {
if (selectedTabIndex==i) {
UILabel *tabBarLabel = [tabBarLabelsArray objectAtIndex:i];
tabBarLabel.textColor = [UIColor whiteColor];
continue;
}
UIImage *buttonImage = [tabBarImagesArray objectAtIndex:i];
UIButton *button = [tabBarButtonsArray objectAtIndex:i];
[button setImage:buttonImage forState:UIControlStateNormal];
[button setImage:buttonImage forState:UIControlStateSelected];
[button setImage:buttonImage forState:UIControlStateHighlighte
d];
[button setImage:buttonImage forState:UIControlStateSelected|UIControlStateHighlighte
d];
UILabel *tabBarLabel = [tabBarLabelsArray objectAtIndex:i];
tabBarLabel.textColor = [UIColor blackColor];
}
UIButton *selectedButton = [tabBarButtonsArray objectAtIndex:selectedTabIndex];
UIImage *buttonImage = [tabBarSelectedStateImage
sArray objectAtIndex:selectedTabIndex];
[selectedButton setImage:buttonImage forState:UIControlStateNormal];
[selectedButton setImage:buttonImage forState:UIControlStateSelected];
[selectedButton setImage:buttonImage forState:UIControlStateHighlighte
d];
[selectedButton setImage:buttonImage forState:UIControlStateSelected|UIControlStateHighlighte
d];
}
float tabWidth = self.tabBar.frame.size.width/tabItemsCount;
float tabXCenter = tabWidth/2;
float tabYCenter = self.tabBar.frame.size.height-7;
for (int i=0;i < [self.viewControllers count]; i++) {
UILabel *tabBarLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0,tabWidth, 50)];
tabBarLabel.text = [tabBarTitlesArray objectAtIndex:i];
tabBarLabel.center = CGPointMake(tabXCenter, tabYCenter);
tabBarLabel.backgroundColor = [UIColor clearColor];
tabBarLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:10];
tabBarLabel.textAlignment = UITextAlignmentCenter;
tabXCenter += tabWidth;
[self.tabBar addSubview:tabBarLabel];
[tabBarLabelsArray addObject:tabBarLabel];
[tabBarLabel release];
}
}