1.创建UITabBarController
@implementation RootViewController{
NSMutableArray *_allBtnArr; //按钮数据数组
}
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //隐藏系统tabBar self.tabBar.hidden = YES; [self initailData]; [self createSubViews]; } //初始化数据 - (void)initailData{ _allBtnArr = [NSMutableArray array]; NSDictionary *btn1Dic = [NSDictionary dictionaryWithObjectsAndKeys:@"首页0" , @"title", @"1.png" , @"image", nil]; NSDictionary *btn2Dic = [NSDictionary dictionaryWithObjectsAndKeys:@"首页1" , @"title", @"1.png" , @"image", nil]; NSDictionary *btn3Dic = [NSDictionary dictionaryWithObjectsAndKeys:@"首页2" , @"title", @"1.png" , @"image", nil]; NSDictionary *btn4Dic = [NSDictionary dictionaryWithObjectsAndKeys:@"首页3" , @"title", @"1.png" , @"image", nil]; [_allBtnArr addObject:btn1Dic]; [_allBtnArr addObject:btn2Dic]; [_allBtnArr addObject:btn3Dic]; [_allBtnArr addObject:btn4Dic]; } //初始化视图(添加按钮) - (void)createSubViews{
//TabBar背景 UIView *backgroundV = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 49, self.view.frame.size.width, 49)]; backgroundV.backgroundColor = [UIColor purpleColor]; [self.view addSubview:backgroundV]; //循环创建自定义按钮 NSInteger i = 0; for (NSDictionary *dic in _allBtnArr) { RootUIButton *tabBarBtn = [[RootUIButton alloc] initWithTitle:[dic objectForKey:@"title"] WithImage:[UIImage imageNamed:[dic objectForKey:@"image"]] WithFrame:CGRectMake(backgroundV.frame.size.width / _allBtnArr.count * i, 0, backgroundV.frame.size.width / _allBtnArr.count, backgroundV.frame.size.height)]; tabBarBtn.backgroundColor = [UIColor clearColor]; tabBarBtn.tag = 1000 + i; [tabBarBtn addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside]; [backgroundV addSubview:tabBarBtn]; i++; } }
//根据Tag值标记接下来的操作 - (void)changeViewController:(UIButton *)button{ switch (button.tag) { case 1000: NSLog(@"0");
self.selectedIndex = 0; break; case 1001: NSLog(@"1");
self.selectedIndex = 1; break; case 1002: NSLog(@"2");
self.selectedIndex = 2; break; case 1003: NSLog(@"3");
self.selectedIndex = 3; break; default: break; } }
2.创建UIButton
//自定义按钮初始化 标题 图片 尺寸 - (instancetype)initWithTitle:(NSString *)title WithImage:(UIImage *)image WithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { [self createSubViewsWithTitle:title WithImage:image]; } return self; } - (void)createSubViewsWithTitle:(NSString *)title WithImage:(UIImage *)image{ //创建按钮图片 UIImageView *btnImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 20)]; btnImageV.backgroundColor = [UIColor clearColor]; btnImageV.image = image; btnImageV.contentMode = 1; [self addSubview:btnImageV]; //创建按钮文字 UILabel *titleLB = [[UILabel alloc] initWithFrame:CGRectMake(btnImageV.frame.origin.x, btnImageV.frame.origin.y + btnImageV.frame.size.height, btnImageV.frame.size.width, 20)]; titleLB.backgroundColor = [UIColor cyanColor]; titleLB.textAlignment = NSTextAlignmentCenter; titleLB.text = title; [self addSubview:titleLB]; }
3.在AppDelegate中使用自定义的UITabBarViewController
ViewController *homeVC = [[ViewController alloc] init]; TwoViewController *twoVC = [[TwoViewController alloc] init]; ThreeViewController *threeVC = [[ThreeViewController alloc] init]; FourViewController *foureVC = [[FourViewController alloc] init]; RootViewController *rootTabBarVC = [[RootViewController alloc] init]; rootTabBarVC.viewControllers = [NSArray arrayWithObjects:homeVC, twoVC, threeVC, foureVC, nil]; self.window.rootViewController = rootTabBarVC;
效果图: