- (void)viewDidLoad {
[super viewDidLoad];
[self creatVC];
[self creatTabBar];
NSLog(@"tabbar");
}
- (void)creatVC{
StudentViewController * svc = [[StudentViewController alloc] init];
TeachViewController *tvc = [[TeachViewController alloc] init];
CarFirstPageViewController *cvc = [[CarFirstPageViewController alloc] init];
RecruitViewController *rvc = [[RecruitViewController alloc] init];
MineViewController *mvc = [[MineViewController alloc] init];
UINavigationController *snc = [[UINavigationController alloc] initWithRootViewController:svc];
UINavigationController *tnc = [[UINavigationController alloc] initWithRootViewController:tvc];
UINavigationController *cnc = [[UINavigationController alloc] initWithRootViewController:cvc];
UINavigationController *rnc = [[UINavigationController alloc] initWithRootViewController:rvc];
UINavigationController *mnc = [[UINavigationController alloc] initWithRootViewController:mvc];
self.viewControllers = [NSArray arrayWithObjects:snc, tnc, cnc, rnc, mnc, nil];
}
- (void)creatTabBar{
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"底部导航背景"]];
iv.userInteractionEnabled = YES;
[self.tabBar addSubview:iv];
NSArray *titleArr = @[@"我的学员",@"教学",@"约车",@"招生",@"我的"];
NSArray *imageArr = @[@"wodexueyuan",@"jiaoxue",@"zhaosheng",@"wode"];
for (int i = 0; i < 5; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
if (i == 2) {
btn.frame = CGRectMake(ScreenWidth/2-25, -12, 50, 44);
[btn setBackgroundImage:[UIImage imageNamed:@"yuechetubiao@2x"] forState:(UIControlStateNormal)];
btn.tag = i;
[btn setAdjustsImageWhenHighlighted:NO];
[iv addSubview:btn];
_label = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth/2-25, 32, 50, 15)];
_label.text = titleArr[i];
_label.textAlignment = NSTextAlignmentCenter;
_label.textColor = RGB(153, 153, 153, 1);
_label.font = [UIFont systemFontOfSize:11];
_label.tag = 121;
[iv addSubview:_label];
}else{
btn.frame = CGRectMake(ScreenWidth/5*i+(ScreenWidth/5-30)*0.5, 3, 30, 30);
_label = [[UILabel alloc] initWithFrame:CGRectMake(-10, 29, 50, 15)];
if (i > 2) {
[btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_%@_unselected",imageArr[i-1]]] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_%@_selected",imageArr[i-1]]] forState:UIControlStateSelected];
_label.text = titleArr[i];
}else{
[btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_%@_unselected",imageArr[i]]] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_%@_selected",imageArr[i]]] forState:UIControlStateSelected];
_label.text = titleArr[i];
}
btn.tag = i;
[iv addSubview:btn];
_label.textAlignment = NSTextAlignmentCenter;
_label.textColor = RGB(153, 153, 153, 1);
_label.font = [UIFont systemFontOfSize:11];
[btn addSubview:_label];
}
if (i == 1 || i == 4) {
UIImageView *dotImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tishixiaohongdian"]];
dotImage.backgroundColor = [UIColor clearColor];
CGFloat x = btn.right;
CGFloat y = btn.top;
dotImage.frame =CGRectMake(x, y, 8,8);
dotImage.tag = 1000+i;
[iv addSubview:dotImage];
}
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
if (i == 0) {
btn.selected = YES;
_label.textColor = [UIColor colorWithRed:210/255.0 green:19/255.0 blue:32/255.0 alpha:1];
_tmpBtn = btn;
_tmpLabel = _label;
}
}
}
- (void)btnClick:(UIButton *)sender{
if (_tmpBtn == sender) {
return;
}
_tmpBtn.selected = NO;
_tmpLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
if (sender.tag != 2) {
sender.selected = YES;
UILabel *label = (UILabel *)[sender.subviews lastObject];
label.textColor = [UIColor colorWithRed:210/255.0 green:19/255.0 blue:32/255.0 alpha:1];
_tmpLabel = label;
}else{
UILabel *label = (UILabel *)[self.tabBar viewWithTag:121];
label.textColor = [UIColor colorWithRed:210/255.0 green:19/255.0 blue:32/255.0 alpha:1];
_tmpLabel = label;
}
_tmpBtn = sender;
self.selectedIndex = sender.tag;
}