自定义TabBar的尝试(1)

来源:互联网 发布:vmware12安装linux系统 编辑:程序博客网 时间:2024/06/05 15:38

看到了其他人做过的自定义TabBar,然后想自己也去搞搞看


先来个初步的效果图


TabBar中主要是用几个UIButton贴在View上实现的


首先设置下背景View


    self.backgroundColor = [UIColorcolorWithRed:248.0f/255.0fgreen:248.0f/255.0fblue:248.0f/255.0falpha:1];

    self.layer.shadowColor = [[UIColorgrayColor]CGColor];

    self.layer.shadowOffset =CGSizeMake(-1, -1);

    self.layer.shadowOpacity =0.3f;

    self.layer.shadowRadius =4;

    self.alpha =0.8f;


for循环创建TabBarItem,可以自定义title和数量

    selectImgArr = [[NSMutableArrayalloc]init];

    unSelectImgArr = [[NSMutableArrayalloc]init];

    for (int i =0; i < itemCount; i++) {

        [selectImgArraddObject:[[UIImagealloc]init]];

        [unSelectImgArraddObject:[[UIImagealloc]init]];

        UIButton* itemBtn;

        if (i ==2) {

            itemBtn = [UIButtonbuttonWithType:UIButtonTypeContactAdd];

            itemBtn.selected =YES;

        }else

        {

            itemBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

            [itemBtn setTitle:[titleArrayobjectAtIndex:i]forState:UIControlStateNormal];

        }

        [itemBtn.titleLabelsetFont:[CrewCommonDealgetFont:11]];

        

        [itemBtn setTitleColor:[UIColorgrayColor]forState:UIControlStateNormal];

        [itemBtn setTitleColor:[UIColorredColor]forState:UIControlStateHighlighted];

        itemBtn.tag =1000+i;

        itemBtn.frame =CGRectMake(i*kMainScreen_Width/ItemCount,0,kMainScreen_Width/ItemCount,ItemH);

        [itemBtn addTarget:selfaction:@selector(selectItem:)forControlEvents:UIControlEventTouchUpInside];

        [selfaddSubview:itemBtn];

    }


控制没个Button的位置

itemBtn.frame =CGRectMake(i*kMainScreen_Width/ItemCount,0,kMainScreen_Width/ItemCount,ItemH);


对UIButton图片进行设置并使用UIEdgeInsets调整图片与title对应位置 


之后建立delegate,设定选中状态,在VC中通过Action调用原生tabbarVC实现不同VC的跳转

self.selectedIndex = itemTag;


0 0
原创粉丝点击