iOS-UITabbar自定义

[self createCustomTabBar];

-(void)createCustomTabBar{
    //创建一个UIImageView,作为底图
    UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,[UIScreen mainScreen].bounds.size.height-49, 320, 49)];
    bgView.image = [UIImage imageNamed:@"tabbg.png"];
    bgView.tag = 999;
    //开启imageview的交互属性
    bgView.userInteractionEnabled = YES;
    [self.view addSubview:bgView];
   
    
    //创建button实例  模拟tabBarItem
    //每个button的间
    NSArray *array=[NSArray arrayWithObjects:@"IconSettings",@"IconProfile",@"IconHome",@"IconEmpty",@"IconCalendar",@"btn_bottom1_on.png",@"btn_bottom2_on.png",@"btn_bottom3_on.png",@"btn_bottom4_on.png",@"btn_bottom5_on.png", nil];
    for (int i = 0; i<(array.count)/2; i++) {
        NSString *imageName = [array objectAtIndex:i];
        NSString *selectedImage = [array objectAtIndex:i+5];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake(0+64*i, 0, 64, 49)];
        //设置button处于常规状态下的背景图片
        [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        //设置button处于选中状态下得背景图片
        [btn setBackgroundImage:[UIImage imageNamed:selectedImage] forState:UIControlStateSelected];
        [btn addTarget:self
                action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = i;
        
        if (i==0) {
            btn.selected = YES;
        }
        
        [bgView addSubview:btn];
    }
}
-(void)btnClicked:(UIButton *)btn
{
    //点击不同的按钮,切换不同的视图控制器
    self.selectedIndex = btn.tag;
    //切换不同btn的显示状态
    UIImageView *bgView = (UIImageView *)[self.view viewWithTag:999];
    for (UIView *subView in bgView.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            UIButton *btn1 = (UIButton *)subView;
            if (btn1.tag == btn.tag) {
                btn1.selected = YES;
            }else{
                btn1.selected = NO;
            }
        }
    }
}

原文地址:https://www.cnblogs.com/linxiu-0925/p/5422113.html