#import "ViewController.h"
/*
UITabBarController有多少個按鈕,就有多少個子控制器;點擊按鈕切換不同的子控制器
*/
@interface ViewController ()
// 占位的View
@property (weak, nonatomic) IBOutlet UIView *containerView;
// tableBar
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (nonatomic , weak) UIButton *selectBtn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 添加子控制器
[self addChildVc];
// 添加TabBar的按鈕
[self addButton];
}
- (void)addChildVc
{
UIViewController *vc1 = [[UIViewController alloc]init];
vc1.title = @"全部";
vc1.view.backgroundColor = [UIColor redColor];
[self addChildViewController:vc1];
UIViewController *vc2 = [[UIViewController alloc]init];
vc2.title = @"視頻";
vc2.view.backgroundColor = [UIColor blueColor];
[self addChildViewController:vc2];
UIViewController *vc3 = [[UIViewController alloc]init];
vc3.title = @"聲音";
vc3.view.backgroundColor = [UIColor grayColor];
[self addChildViewController:vc3];
UIViewController *vc4 = [[UIViewController alloc]init];
vc4.title = @"段子";
vc4.view.backgroundColor = [UIColor yellowColor];
[self addChildViewController:vc4];
}
- (void)addButton
{
NSInteger count = self.view.subviews.count;
CGFloat btnX = 0;
CGFloat btnY = 0;
CGFloat btnW = self.bottomView.bounds.size.width / count;
CGFloat btnH = self.bottomView.bounds.size.height;
for (int i = 0; i < count; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btnX = i *btnW;
btn.tag = i;
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
UIViewController *vc = self.childViewControllers[i];
[btn setTitle:vc.title forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
[self.bottomView addSubview:btn];
// 監(jiān)聽按鈕點擊
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
// 默認選中第一按鈕
if (btn.tag == 0) {
[self btnClick:btn];
}
}
}
- (void)btnClick:(UIButton *)btn
{
// 切換界面
NSInteger i = btn.tag;
UIViewController *vc = self.childViewControllers[i];
[self.containerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
// 設(shè)置按鈕選中狀態(tài)
self.selectBtn.selected = NO;
btn.selected = YES;
self.selectBtn = btn;
[self.containerView addSubview:vc.view];
}
@end
Story搭建框架:
最終效果: