IOS 自定义tabbar

家好,这么晚了,因为我昨天再接过的项目是,发现外包的伙伴们写的Tabbar是系统的,因为我们的产品要求的是44的高度,而系统的Tabbar的高度是49像素,所以怎么办呢,外包的伙伴们做的是在系统的Tabbar的Item上做的视图覆盖,这样我发现有白色的5像素视图会在iPhone5一下的设备并且是IOS7一下的系统上出现,那么怎么办呢,当然做好的办法是不是补补改改,最好的就是从跟上解决,这样我们框架上的其他东西就不会导致一些问题的出现,所以就要自定义Tabbar,

系统给了一个好的方法:切换视图控制器的:

- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(5_0);

//这个要注意的一点事一定要判断是否finished 做一个BOOL判断,如果没有判断,你要快速的点击切换,会出现白屏,因为你的某一个Controller有refresh或者reload之类的,影响切换的速度,所以一定要做个bool值判断,下来这家看代码吧:

- (void)viewDidLoad

{

    

    [superviewDidLoad];

 

    isTransted YES;

    //主控制器的内容视图

    _contentView = [[UIView allocinitWithFrame:CGRectMake(00self.view.widthself.view.height 44)];

    [self.viewaddSubview:_contentView];

    

    //主控制器的

    _toolView = [[UIView allocinitWithFrame:CGRectMake(0self.view.height 44self.view.width,44)];

    _toolView.backgroundColor = [UIColorredColor];

    _toolView.autoresizingMask UIViewAutoresizingFlexibleTopMargin;

    [self.view addSubview:_toolView];

 

    messages = [[Messagesallocinit];

    Grade *grade  = [[Grade allocinit];

    PhotoTakeCenterVC *tvc = [[PhotoTakeCenterVCalloc]init];

    ListenClassListController *listen = [[ListenClassListControlleralloc]init];

    Setting *setting = [[Setting allocinit];

    

    [selfaddChildViewController:messages];

    [selfaddChildViewController:grade];

    [selfaddChildViewController:tvc];

    [selfaddChildViewController:listen];

    [selfaddChildViewController:setting];

 

    UIViewController *firstController = self.childViewControllers[0];

    _selectedIndex 1;

    firstController.view.top 0;

 

 

 for (NSInteger i = 0; i < count; i ++)

    {

        CGFloat width  = 60;

        CGFloat height = 44;

        CGFloat y = 524 AutoHeight;

        CGFloat x = (width + 1)*i;

        

        if (i == 2)

        {

            width = 76;

            height = 54;

            y -= 10;

        }

        else if (i > 2)

        {

            x += 15;

        }

        

        CGFloat lineY = y;

        

        if (i == 2)

        {

            lineY += 10;

        }

        

        

        UIView *lineView = [[UIView allocinitWithFrame:CGRectMake(x + width, 02, height)];

        lineView.backgroundColor COLOR_WITH_RGB(28.0f30.0f36.0f);

        [_toolView addSubview:lineView];

        [lineView release];

        

        if (iPhone5) {

            y = 568 44;

        }

        else

        {

            y = 480 44;

        }

        

        if (i != 2) {

            BarItem *button = [[BarItem allocinitWithFrame:CGRectMake(x, 0, width, height)];

            NSString *normal = [NSString stringWithFormat:@"TabItem%d_0",i];

            [button setImage:LOADIMAGE(normal, @"png"forState:UIControlStateNormal];

            

            NSString *selected = [NSString stringWithFormat:@"NewTabItem%d_1",i];

            [button setImage:LOADIMAGE(selected, @"png"forState:UIControlStateSelected];

            button.badgeValue @"0";

            button.tag = i + ITEM_TAG 999;

            [_toolView addSubview:button];

            if (i == 0)

            {

                button.selected YES;

                [[NSNotificationCenterdefaultCenterpostNotificationName:@"UPDATE_ITEM_BADGEVALUE"object:chattype_chat];

            }

            [button release];

            

            [button addTarget:selfaction:@selector(selectControl:) forControlEvents:UIControlEventTouchUpInside];

            self.selectedIndex 1;

            

        }

 

        if(i == 2)

        {

            

            BarItem *button = [[BarItem allocinitWithFrame:CGRectMake(x, -10, width, height)];

            NSString *normal = [NSString stringWithFormat:@"TabItem%d_0",i];

            [button setImage:LOADIMAGE(normal, @"png"forState:UIControlStateNormal];

            

            NSString *selected = [NSString stringWithFormat:@"NewTabItem%d_1",i];

            [button setImage:LOADIMAGE(selected, @"png"forState:UIControlStateSelected];

            button.badgeValue @"0";

            button.tag = i + ITEM_TAG -999;

            [_toolView addSubview:button];

            [button addTarget:selfaction:@selector(takePhoto) forControlEvents:UIControlEventTouchUpInside];

            [button release];

        }

        

    }

 [_contentView addSubview:firstController.view];

}

 

- (void)selectControl:(BarItem *)sender

{

    if (isTransted == NO) {

        return;

    }

    ;

    if (_selectedIndex != sender.tag)

    {

        isTransted NO;

        UIViewController *toViewController = self.childViewControllers[sender.tag 1];

        toViewController.view.top 0;

        

    

        [selftransitionFromViewController:self.childViewControllers[_selectedIndex 1toViewController:toViewController duration:0options:0animations:^{

            

        } completion:^(BOOL finished) {

            isTransted YES;

        }];

        

        //变换控制器

        _selectedIndex = sender.tag;

    }

   

    for (BarItem *btn in_toolView.subviews)

    {

        if ([btn isKindOfClass:[BarItem class]])

        {

            btn.selected NO;

            NSString *normal = [NSString stringWithFormat:@"TabItem%d_0",btn.tag - 1];

            [btn setImage:LOADIMAGE(normal, @"png"forState:UIControlStateNormal];

        }

    }

    

    

    BarItem *btn1 = (BarItem *)[_toolView viewWithTag:sender.tag];

    NSString *selected = [NSString stringWithFormat:@"NewTabItem%d_1",sender.tag - 1];

    [btn1 setImage:LOADIMAGE(selected, @"png"forState:UIControlStateNormal];

    btn1.selected YES;

}

 

 

这里的BarItem是一个自定义的button

 

大家尝试吧。其实还有比这更好的,那个是封装的,资源code4App里有。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值