TabBarController的定制

大多数时候,我们做项目的时候因为UI设计的需要或者美观的需要,再或者显示我们公司或项目的与众不同,我们会对TabBarController做一些定制;当然为了方便快捷的定制,我们一般会使用xib来实现;
  这里具体怎么画是个人的喜好,我在这里只写相关代码部分。
  第一步,先要有一个继承于UITabBarController的YLMyTabBarController(包含xib)
  然后我们定制好视图后就进入下一步,代码实现部分

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBar.hidden=YES;//把视图原有的tabbar藏起来
 //===================================================
    //第一个参数是xib文件的名字,第二个参数加载的视图的所有者是;第三个参数是相关的数据;
    //返回的是一个数组:里面装的是从xib文件中加载的所有的视图(可能有做了很多视图)
   NSArray *array=[[NSBundle mainBundle]loadNibNamed:@"YLMyTabBarview" owner:self options:nil];
    tabBarView=[array firstObject];
    tabBarView.frame=CGRectMake(0, self.view.bounds.size.height-tabBarView.bounds.size.height, self.view.bounds.size.width, tabBarView.bounds.size.height);
   //将自己制作的view取出来加载到视图上-   
 [self.view addSubview:tabBarView];
 
    //给自己视图上的button绑定回调
    for (UIView *temp in tabBarView.subviews) {
        if (temp.tag>=200&&temp.tag<=203) {
            UIButton *tempButton=(id)temp;
            [tempButton addTarget:self action:@selector(tabButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}

回调和父类方法实现

-(void)tabButtonClicked:(UIButton*)sender{
    self.selectedIndex=sender.tag-200;
    for (UIView *temp in tabBarView.subviews) {
        if (temp.tag>=200&&temp.tag<=203) {
            UIButton *tempButton=(id)temp;
            tempButton.selected=NO;
        }
    }
}
-(void)setSelectedIndex:(NSUInteger)selectedIndex{
    //重写父类方法是
    [super setSelectedIndex:selectedIndex];
}

具体实现

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //自定义的
    UITabBarController *tab=[[YLMyTabBarController alloc]init];
    tab.delegate=self;
    //
    
    NSArray *colors=@[[UIColor blueColor],[UIColor orangeColor],[UIColor purpleColor],[UIColor greenColor]];
    //当分栏超过5栏后会建立一个表格视图,最好不要超过5栏
    NSMutableArray *vcs=[NSMutableArray array];
    for (int i=0; i<colors.count; i++) {
        YLViewController *testVC=[[YLViewController alloc]init];
        testVC.title=[NSString stringWithFormat:@"第%d栏",i+1];
        testVC.bgColor=colors[i];
        //加导航
        UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:testVC];
        [vcs addObject:nav];
    }
    //
    tab.viewControllers=vcs; 
    _window.rootViewController=tab;
    

    
    
    return YES;
}
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,231评论 4 61
  • 2016年7月16日,傍晚十分妈妈在做饭,理想到厨房门口说:妈妈,做饭就要带上围裙,要不衣服弄脏了还要用肥皂洗,多...
    相遇至此阅读 2,286评论 0 0
  • 不同的平台,输入设备时不一样的,桌面应用可以使用键盘和鼠标与程序进行沟通,基于浏览器的应用也一样,在Android...
    天神Deity阅读 3,130评论 0 0
  • 这是大部分人的行程安排——顺时针的大环线。 D1:从西宁出发,经塔尔寺、日月山、倒淌河、青海湖、环湖西路到达黑马河...
    鱼头shiny阅读 3,012评论 0 0
  • 坊间流传新四俗:从小有个音乐梦,辞职开间咖啡馆,改变世界要创业,放下一切去旅行。而我要很不知趣的说:一次远行拯救...
    嘟嘟的小二阅读 2,144评论 0 0