自定义标签栏和按钮的点击切换事件

#import "MainViewController.h"

#import "CommonInfoViewController.h"

#import "MyViewController.h"

#import "BaseNavigationController.h"

 

@interface MainViewController ()

@end

@implementation MainViewController

{

    NSArray *imageNames;  //默认图片数组

    NSArray *selectImageNames;  //选中图片数组

    UIButton *selectBt;  //记录选中按钮

}

 

 

- (void)viewDidLoad {

    

    [super viewDidLoad];

    

    //1.创建子控制器

    [self _createSubViewCtrls];

    

    //2.自定义标签栏,移除系统tabbar

    [self _removeSystemTabBarItem];

    

    //3.创建自定义tabbarItem

    [self _createTabbarItem];

}

 

 

//创建子控制器

- (void)_createSubViewCtrls{

    

    imageNames = @[@"publicinf_ic",@"my_ic"];

    selectImageNames = @[@"publicinf_press_ic",@"my_press_ic"];

 

    //2.创建第三级控制器

    CommonInfoViewController *commonCtrl = [[CommonInfoViewController alloc] init];

    

    MyViewController *myCtrl = [[MyViewController alloc] init];

    

    NSArray *ctrls =@[commonCtrl,myCtrl];

    

    //存储导航控制器

    NSMutableArray *array = [NSMutableArray arrayWithCapacity:5];

    for (UIViewController *ctrl in ctrls) {

        

        //1.创建导航控制器(子类化导航控制器)

        BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:ctrl];

        //2.把导航控制器加入到数组中

        [array addObject:nav];

    }

    //把导航控制器交给标签控制器管理

    self.viewControllers = array;

}

 

 

//移除系统tabbar按钮

- (void)_removeSystemTabBarItem{

    

    NSLog(@"tabbar的子视图 :%@",self.tabBar.subviews);

    

    for (UIView *view in self.tabBar.subviews) {

        

        Class c = NSClassFromString(@"UITabBarButton");

        

        //判断view是不是由UITabBarButton类创建

        if ([view isKindOfClass:c]) {

            

            [view removeFromSuperview];

        }

    }

}

 

//创建自定义tabbar按钮

- (void)_createTabbarItem{

    

    

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.tabBar.bounds];

    imageView.userInteractionEnabled = YES;

    

    [self.tabBar addSubview:imageView];

    

    

    

    //按钮宽度

    CGFloat width = kSCREEN_WIDTH / imageNames.count;

    

    for (NSInteger i = 0; i < imageNames.count; i++) {

        

        //取出图片名

        NSString *imageName  = imageNames[i];

        

        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(i * width, 0, width, kTabBarHeight)];

        

        [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];

        [btn setImage:[UIImage imageNamed:selectImageNames[i]] forState:UIControlStateSelected];

        

        if (i == 0) {

            btn.selected = YES;

            selectBt = btn;

            

        }

        

        [btn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];

        btn.tag = i + 1;

        

        [imageView addSubview:btn];

        

     

    }

}

 

//按钮点击事件

- (void)clickAction:(UIButton *)btn{

    

    if (btn == selectBt) {

        

        return;

        

    }

    

    self.selectedIndex = btn.tag - 1;

    

    btn.selected = YES;

    selectBt.selected = NO;

    selectBt = btn;

    

}

 

原文地址:https://www.cnblogs.com/pengsi/p/5329307.html