IOS-自定义TabBar
1 // 2 // RQHTabBarController.m 3 // TabBarView 4 // 5 // Created by apple on 15-1-13. 6 // Copyright (c) 2015年 __MyCompanyName__. All rights reserved. 7 // 可以让storyboard中关联这个类,这个类继承UITabBarController 8 9 #import "RQHTabBarController.h" 10 11 @implementation RQHTabBarController 12 @synthesize selectedBtn;//也可以用Bool值来写,用来取反 13 14 - (void)viewDidLoad 15 { 16 17 CGRect rect = self.tabBar.frame;//拿到tabbar的大小 18 [self.tabBar removeFromSuperview];//删除系统的tabbar 19 20 //创建一个View取代tabbar的位置 21 UIView *myView = [UIView new]; 22 myView.frame = CGRectMake(0, rect.origin.y-30, rect.size.width, rect.size.height+30); 23 [self.view addSubview:myView]; 24 25 UIImageView *background = [[UIImageView alloc]initWithFrame:myView.bounds]; 26 background.image = [UIImage imageNamed:@"BackGroundTabBar"]; 27 [myView addSubview:background]; 28 29 for (int i=0; i<3; i++) { 30 UIButton *btn = [UIButton new]; 31 NSString *imageName = [NSString stringWithFormat:@"TabBar%d", i + 1]; 32 NSString *imageNameSel = [NSString stringWithFormat:@"TabBar%dSel", i + 1]; 33 34 [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal]; 35 [btn setImage:[UIImage imageNamed:imageNameSel] forState:UIControlStateSelected]; 36 CGFloat x = i * myView.frame.size.width / 5; 37 btn.frame = CGRectMake(x, 0, myView.frame.size.width / 5, myView.frame.size.height+10); 38 39 [myView addSubview:btn]; 40 41 btn.tag = i;//设置按钮的标记, 方便来索引当前的按钮,并跳转到相应的视图 42 43 //带参数的监听方法记得加"冒号" 44 [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside]; 45 46 //设置刚进入时,第一个按钮为选中状态 47 if (0 == i) { 48 btn.selected = YES; 49 self.selectedBtn = btn; //设置该按钮为选中的按钮 50 } 51 52 } 53 54 55 } 56 57 - (void)clickBtn:(UIButton *)button { 58 //1.先将之前选中的按钮设置为未选中 59 self.selectedBtn.selected = NO; 60 //2.再将当前按钮设置为选中 61 button.selected = YES; 62 //3.最后把当前按钮赋值为之前选中的按钮 63 self.selectedBtn = button; 64 65 //4.跳转到相应的视图控制器. (通过selectIndex参数来设置选中了那个控制器) 66 self.selectedIndex = button.tag; 67 } 68 69 @end
----------------------------------------
我怀念的是我回不去的曾经
----------------------------------------