标签:
第一步:添加两个占位View
第二步:添加子控制器
第三步:添加按钮
#import "ViewController.h" #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width) #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height) #define TabBar_HEIGHT 49 @interface ViewController () /** 底部占位View */ @property(nonatomic, weak)UIView * bottomView; /** 导航占位View */ @property(nonatomic, weak)UIView * TabBarView; /** 记录按钮点击状态 */ /** <#注释#> */ @property(nonatomic, strong) UIButton * tempBtn; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; /** 第一步添加两个占位View */ [self setSpaceView]; /** 添加子控制器 */ [self setSubControlVC]; } #pragma mark -添加子控制器 -(void)setSubControlVC{ UIViewController * vc = ({ vc = [[UIViewController alloc]init]; vc.title = @"全部"; vc.view.backgroundColor = [UIColor yellowColor]; [self addChildViewController:vc]; vc; }); UIViewController * vc1 = ({ vc1 = [[UIViewController alloc]init]; vc1.title = @"全部1"; vc1.view.backgroundColor = [UIColor brownColor]; [self addChildViewController:vc1]; vc1; }); UIViewController * vc2 = ({ vc2 = [[UIViewController alloc]init]; vc2.title = @"全部2"; vc2.view.backgroundColor = [UIColor greenColor]; [self addChildViewController:vc2]; vc2; }); [self setButtonTitle]; } #pragma mark -添加按钮 -(void)setButtonTitle{ /** 计算按钮尺寸 */ NSInteger count = self.childViewControllers.count; CGFloat btnW = 1.0 *SCREEN_WIDTH /count; CGFloat btnH = TabBar_HEIGHT; CGFloat btnX = 0; CGFloat btnY = 0; /** 循环添加按钮 */ for (int i = 0; i < count; i++) { /** 创建按钮 */ UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.tag = i; UIViewController * vc = self.childViewControllers[i]; /** 设置按钮的字体颜色 */ [btn setTitle:vc.title forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected]; btnX = i * btnW; /** 设置位置 */ btn.frame = CGRectMake(btnX, btnY, btnW, btnH); /** 设置点击事件 */ [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; /** 添加按钮到tabView */ [self.TabBarView addSubview:btn]; if (i == 0) { [self btnClick:btn]; } } } -(void)btnClick:(UIButton *)btn{ self.tempBtn.selected = NO; btn.selected = YES; self.tempBtn = btn ; /** 获取子控制器 */ UIViewController* vc = self.childViewControllers[btn.tag]; /** 移除之前的控制器 */ [self.bottomView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; /** 添加当前控制器 */ [self.bottomView addSubview:vc.view]; } #pragma mark -添加view -(void)setSpaceView{ /** 添加底部占位View */ UIView * bottomV =({ bottomV = [[UIView alloc]initWithFrame:self.view.bounds]; self.bottomView = bottomV; self.bottomView.backgroundColor = [UIColor redColor]; [self.view addSubview:self.bottomView]; bottomV; }); /** 导航占位View */ UIView * tabBarV =({ tabBarV = [[UIView alloc]initWithFrame:CGRectMake(0,SCREEN_HEIGHT - TabBar_HEIGHT , SCREEN_WIDTH, TabBar_HEIGHT)]; self.TabBarView =tabBarV; self.TabBarView.backgroundColor = [UIColor orangeColor]; [self.view addSubview:self.TabBarView]; tabBarV; }); } @end
标签:
原文地址:http://www.cnblogs.com/fleas/p/5725854.html