首先创建一个类,继承自UItabBarController
然后在.m文件中:
这里我有两个宏定义:
#define WIDTH (myView.frame.size.width / 4) //我在写4个按钮(即4个标签,所以是所有宽度除以4) #define HEIGHT (myView.frame.size.height)
然后写延展,声明了一个按钮属性,用来记录上一个被点击的按钮(这是用来改变颜色,即点击状态的):
@interface MyTabBarController () //之前选中的按钮 @property(nonatomic, retain) UIButton *selectedBtn; @end
然后开始写自己想要的东西了,在viewDidLoad中:
(1)删除继承父类而来的tabBar,自定义自己想要的视图,其frame为原来tabBar的frame。
(2)然后定义了4个按钮和4个label,即标签和标签下的标题
(3)然后循环给4个按钮添加点击事件,触发事件是同一个clickBtn
- (void)viewDidLoad { [super viewDidLoad]; //删除现有的tabBar CGRect rect = self.tabBar.frame; [self.tabBar removeFromSuperview]; //添加自己的视图 UIView *myView = [[UIView alloc] init]; myView.backgroundColor = [UIColor whiteColor]; myView.frame = rect; [self.view addSubview:myView]; //主页发现按钮 UIButton *findBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [findBtn setImage:[UIImage imageNamed:@"icon_home_n"] forState:UIControlStateNormal]; [findBtn setImage:[UIImage imageNamed:@"icon_home_h"] forState:UIControlStateSelected]; findBtn.tag = 1; findBtn.frame = CGRectMake(0, -20, WIDTH, HEIGHT); //默认选中是发现按钮 findBtn.selected = YES; [findBtn setTintColor:[UIColor colorWithRed:0.886 green:0.365 blue:0.247 alpha:1.000]]; self.selectedBtn = findBtn; [myView addSubview:findBtn]; UILabel *findLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH/2 -10, HEIGHT - 20, WIDTH, 20)]; findLabel.text = @"发现"; findLabel.textColor = [UIColor colorWithRed:0.804 green:0.325 blue:0.227 alpha:1.000]; findLabel.font = [UIFont systemFontOfSize:10]; findLabel.tag = 101; [myView addSubview:findLabel]; [findLabel release]; //搜索页 UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [searchBtn setImage:[UIImage imageNamed:@"icon_search_n"] forState:UIControlStateNormal]; [searchBtn setImage:[UIImage imageNamed:@"icon_search_h"] forState:UIControlStateSelected]; searchBtn.tag = 2; searchBtn.frame = CGRectMake(WIDTH, -20, WIDTH, HEIGHT); [myView addSubview:searchBtn]; UILabel *searchLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH + WIDTH/2 -10, HEIGHT - 20, WIDTH, 20)]; searchLabel.text = @"搜索"; searchLabel.textColor = [UIColor grayColor]; searchLabel.font = [UIFont systemFontOfSize:10]; searchLabel.tag = 102; [myView addSubview:searchLabel]; [searchLabel release]; //收藏页 UIButton *costBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [costBtn setImage:[UIImage imageNamed:@"iconfont-like"] forState:UIControlStateNormal]; [costBtn setImage:[UIImage imageNamed:@"iconfont-like-selected"] forState:UIControlStateSelected]; costBtn.tag = 3; costBtn.frame = CGRectMake(2 * WIDTH, -20, WIDTH, HEIGHT); [myView addSubview:costBtn]; UILabel *collectLabel = [[UILabel alloc] initWithFrame:CGRectMake(2 * WIDTH + WIDTH/2 -10, HEIGHT - 20, WIDTH, 20)]; collectLabel.text = @"收藏"; collectLabel.textColor = [UIColor grayColor]; collectLabel.font = [UIFont systemFontOfSize:10]; collectLabel.tag = 103; [myView addSubview:collectLabel]; [collectLabel release]; //设置页 UIButton *setUpBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [setUpBtn setImage:[UIImage imageNamed:@"icon_setting_n"] forState:UIControlStateNormal]; [setUpBtn setImage:[UIImage imageNamed:@"icon_setting_h"] forState:UIControlStateSelected]; setUpBtn.tag = 4; setUpBtn.frame = CGRectMake(3 * WIDTH, -20, WIDTH, HEIGHT); [myView addSubview:setUpBtn]; UILabel *setUpLabel = [[UILabel alloc] initWithFrame:CGRectMake(3 * WIDTH + WIDTH/2 -10, HEIGHT - 20, WIDTH, 20)]; setUpLabel.text = @"设置"; setUpLabel.textColor = [UIColor grayColor]; setUpLabel.font = [UIFont systemFontOfSize:10]; setUpLabel.tag = 104; [myView addSubview:setUpLabel]; [setUpLabel release]; for (int i = 1; i <4; i++) { //4个按钮的点击事件是一个 UIButton *btn = [self.view viewWithTag:i]; [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside]; } }
(4)然后实现按钮的点击事件clickBtn:实现按钮和其下面标题 选择和未选择状态的切换;也需要实现视图的跳转:
#pragma mark -- 自定义tabBar按钮的点击事件 -(void)clickBtn:(UIButton *)sender { //设置上一个按钮 self.selectedBtn.selected = NO; UILabel *titleFormer = [self.view viewWithTag:self.selectedBtn.tag + 100]; titleFormer.textColor = [UIColor grayColor]; //设置选中状态 sender.selected = YES; self.selectedBtn = sender; UILabel *titleNow = [self.view viewWithTag:self.selectedBtn.tag + 100]; titleNow.textColor = [UIColor colorWithRed:0.804 green:0.325 blue:0.227 alpha:1.000]; //跳转相应的视图控制器 self.selectedIndex = sender.tag - 1; }
然后就基本实现了一个视图控制器的功能了,自己还有什么需求可以往里加,使用就跟视图的标签视图一样使用就行,只是这里的按钮图片我都是在类里就赋值好了;
这是我自定义的tabBarController下载地址,需要的可以下下来参考参考:
http://download.csdn.net/detail/margaret_mo/9451753