#import "JWTabBarButton.h" #import "JWTabBarController.h" #import "ViewController1.h" #import "ViewController2.h" #import "ViewController13.h" #import "ViewController14.h" @interface JWTabBarController () @property (nonatomic,weak) UIButton *selectedButton; @end @implementation JWTabBarController - (void)viewDidLoad { [super viewDidLoad]; [self createControllers]; [self createTabBar]; self.tabBar.backgroundColor = HEXCOLOR(0xffffff); self.tabBar.translucent = NO; // Do any additional setup after loading the view. } - (void)createControllers{ ViewController1 *View1 = [[ViewController1 alloc]init]; ViewController2 *View2 = [[ViewController2 alloc] init]; ViewController3 *View3 = [[ViewController3 alloc] init]; ViewController4 *View4 = [[ViewController4 alloc] init]; NSArray *vcArr = @[View1,View2,View3,View4]; NSMutableArray *navVCArr = [NSMutableArray array]; for (int i = 0; i < vcArr.count; i++) { JWNavigationController *nav = [[JWNavigationController alloc] initWithRootViewController:vcArr[i]]; [navVCArr addObject:nav]; } self.viewControllers = navVCArr; } /** * 建立TabBar */ - (void)createTabBar{ // 1 移除TabbarButton for (UIView *child in self.tabBar.subviews) { if ([child isKindOfClass:[UIControl class]]) { [child removeFromSuperview]; } } // 2 建立button NSArray *titles = @[@"View1",@"View2",@"View3",@"View4"]; for (NSInteger i = 0; i < titles.count; i++) { JWTabBarButton *button = [JWTabBarButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake((WIDTH/3.0) * i, 0, WIDTH/3.0, 49); [button setTitle:titles[i] forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal]; [button setTitleColor:HEXCOLOR(0x6b6b6b) forState:UIControlStateNormal]; [button setTitleColor:HEXCOLOR(0xff645f) forState:UIControlStateSelected]; button.titleLabel.textAlignment = NSTextAlignmentCenter; button.titleLabel.font = [UIFont boldSystemFontOfSize:9]; NSString *selectedImage = [NSString stringWithFormat:@"%@_selected",images[i]]; UIImage *image = [UIImage imageNamed:selectedImage]; [button setImage:image forState:UIControlStateSelected]; [button setImage:image forState:UIControlStateHighlighted]; button.tag = 1000 + i; button.alpha = 1; [button addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside]; if (i == self.tabBarSelectIndex ) { button.selected = YES; self.selectedButton = button; }else{ button.selected = NO; } [self.tabBar addSubview:button]; } } - (void)selectAction:(JWTabBarButton *)button{ if (button.tag != self.selectedButton.tag) { self.selectedButton.selected = NO; button.selected = YES; self.selectedButton = button; self.selectedIndex = self.selectedButton.tag - 1000; } self.tabBarSelectIndex = (NSInteger)self.selectedButton.tag -1000; } - (void)setTabBarSelectIndex:(NSInteger)tabBarSelectIndex{ _tabBarSelectIndex = tabBarSelectIndex; if (self.selectedButton.tag -1000 != tabBarSelectIndex) { JWTabBarButton * btn = (JWTabBarButton *)[self.tabBar viewWithTag:tabBarSelectIndex+1000]; btn.selected = YES; self.selectedButton.selected = NO; self.selectedButton = btn; } self.selectedIndex = _tabBarSelectIndex; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } =========================================================== #import "JWTabBarButton.h" @implementation JWTabBarButton /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ - (CGRect)imageRectForContentRect:(CGRect)contentRect { return CGRectMake((contentRect.size.width - 24) / 2, 9, 24, 24); } - (CGRect)titleRectForContentRect:(CGRect)contentRect { return CGRectMake(contentRect.size.width * .15, 37, contentRect.size.width * .7, 11); } @end