电商类的项目像某宝某东的app 都有一个tabbar的平铺导航模式
根据一些demo中案例,个人觉得比较简单易用的自定义的tabbar
1.自定义一个CMTabbarController
#import <UIKit/UIKit.h>
@interface CMMainTabbarController : UITabBarController
self.tabBar.hidden = YES;
2.创建视图控制器
-(void)setController
{
//建议3-5个子视图为宜
ViewController1 *vc1 = [[ViewController1 alloc] init];
//通过此方法把视图加到子视图控制器中
[self addChildViewController:vc1 title:@""];
}
-(void)addChildViewController:(UIViewController *)childController title:(NSString *)title
{
//根据传递来的title,判断是否每个字界面都需要加导航栏,
if ([title isEqualToString:@""]) {
[self addChildViewController:childController];
}else{
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:childController];
childController.navigationItem.title = title;
// 添加导航栏的背景颜色
[nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"sy2@2x"] forBarMetrics:UIBarMetricsDefault];
[self addChildViewController:nav];
}
}
3.创建tabbar按钮
-(void)creatBar:(NSInteger)controllerNum
{
//图片barbtm按钮图片集
NSArray *bar_normal = @[];
NSArray *bar_selected = @[];
//声明一个bar区域
UIImageView *tabbarView = [[UIImageView alloc] initWithFrame:self.tabBar.frame];
tabbarView.user<span style="font-family: Arial, Helvetica, sans-serif;">InteractionEnabled = YES;//十分必要</span>
for (int i = 0; i<self.viewControllers.count; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(EACH_W(self.viewControllers.count)*i, 0, EACH_W(self.viewControllers.count), EACH_H);
[btn setImage:[UIImage imageNamed:bar_normal[i]] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:bar_selected[i]] forState:UIControlStateSelected];
//对btn识别
btn.tag = i+1000;
[tabbarView addSubview:btn];
//设置显示的子视图,默认为首个添加进去的子视图
if (btn.tag == 1000) {
[self btnSelected:btn];
}
[btn addTagret:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
}
[self.view addSubview:tabbarView];
}
-(void)btnSelected:(UIButton *)sender
{
NSLog(@"被点了");
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.selected =NO ;
sender.selected = YES;
button = sender;
self.selectedIndex = sender.tag-1000;
}