CustomTabBarController

.h

#import <UIKit/UIKit.h>

@interface ECGCustomTabBarController : UITabBarController

//注意:数组是根据实际tabbar的视图控制器顺序创建的,顺序不能乱; 而接下来的normalImageNames和disabledImageNames与视图控制器数组三者之间都是一一对应,不能错序;   num是Tabbar需要展示几个视图控制器
@property(nonatomic,assign)int countNum;
@property(nonatomic, strong)NSArray *contentViewControllers;//需要展示的视图控制器数组
@property(nonatomic, strong)NSArray *normalImageNames;//正常Tabbar的items图片数组
@property(nonatomic, strong)NSArray *disabledImageNames;禁用状态Tabbar的items图片数组
@property(nonatomic, strong)NSArray *titles;//Tabbar的items标题数组
@property(nonatomic, assign)int indexTabBarButton;//选中Tabbar的某个索引下的按钮
@property(nonatomic, strong)UILabel *badgeLabel;//角标
/**
 *  <#Description#>
 *
 *  @param b <#b description#>
 */

//NO.1 --->配合属性使用
- (void)showTabbar;

//NO.2
- (void)getWithViewControllers:(NSArray *)viewControllers number:(int)num normalImageNames:(NSArray *)normalImageNames disabledImageNames:(NSArray *)disabledImageNames titles:(NSArray *)titles;

//隐藏
- (void)hideBottomBar:(BOOL)b;
@end


.m

#import "ECGCustomTabBarController.h"
#import "ECGTabBarButton.h"
#import "ECGConst.h"
#import "ECGMacro.h"


@interface ECGCustomTabBarController (){
    UIImageView *_tabBarView; //自定义的覆盖原先的tarbar的控件
    
    ECGTabBarButton *_previousBtn; //记录前一次选中的按钮
    UIView*bottomView;
    NSArray *controllers;
    NSArray *normalImages;
    NSArray *disabledImages;
    NSArray *tabButtonTitles;
    int number;
    NSMutableArray *navArray;//存放创建好的导航控制器
}

@end

@implementation ECGCustomTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)getWithViewControllers:(NSArray *)viewControllers number:(int)num normalImageNames:(NSArray *)normalImageNames disabledImageNames:(NSArray *)disabledImageNames titles:(NSArray *)titles{
    controllers = viewControllers;
    normalImages = normalImageNames;
    disabledImages = disabledImageNames;
    tabButtonTitles = titles;
    number = num;
    [self loadTabBar];
    [self loadVC];
    
}

- (void)setCountNum:(int)countNum{
    if (countNum >= 0) {
        _countNum = countNum;
        number = _countNum;
    }
}


- (void)setContentViewControllers:(NSArray *)contentViewControllers{
    if (contentViewControllers && [contentViewControllers count] > 0) {
        _contentViewControllers = contentViewControllers;
        controllers = _contentViewControllers;
    }
}

- (void)setNormalImageNames:(NSArray *)normalImageNames{
    if (normalImageNames && [normalImageNames count] > 0) {
        _normalImageNames = normalImageNames;
        normalImages = _normalImageNames;
    }
}

- (void)setDisabledImageNames:(NSArray *)disabledImageNames{
    if (disabledImageNames && [disabledImageNames count] > 0) {
        _disabledImageNames = disabledImageNames;
        disabledImages = _disabledImageNames;
    }
}

- (void)setTitles:(NSArray *)titles{
    if (titles && titles.count > 0) {
        _titles = titles;
        tabButtonTitles = _titles;
    }
}

- (void)setIndexTabBarButton:(int)indexTabBarButton{
    if (indexTabBarButton >= 0) {
        _indexTabBarButton = indexTabBarButton;
        if (_tabBarView) {
            ECGTabBarButton *btn = _tabBarView.subviews[_indexTabBarButton];
            [self changeViewController:btn]; //自定义的控件中的按钮被点击了调用的方法,自定义跳转
        }
    }
}

- (void)showTabbar{
    [self loadTabBar];
    [self loadVC];
}


- (void)loadTabBar{
    if ([controllers count]<=0) {
        return;
    }
    self.view.backgroundColor = [UIColor whiteColor];
    self.tabBar.hidden = YES; //隐藏原先的tabBar
    CGFloat tabBarViewY = self.view.frame.size.height - TabBarHeight;
    
    _tabBarView = [[UIImageView alloc] initWithFrame:CGRectMake(0, tabBarViewY+1.5, SCREEN_WIDTH, TabBarHeight)];
    _tabBarView.userInteractionEnabled = YES; //这一步一定要设置为YES,否则不能和用户交互
    _tabBarView.backgroundColor = RGBACOLOR(233, 230, 225, 1.0);
    bottomView=[[UIView alloc]initWithFrame:CGRectMake(0, tabBarViewY, SCREEN_WIDTH, 1.5)];
    bottomView.backgroundColor=[UIColor lightGrayColor];
    [self.view addSubview:bottomView];
    
    [self.view addSubview:_tabBarView];
    
    // 下面的方法是调用自定义的生成按钮的方法
    navArray = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i<number; i++) {
        [self creatButtonWithNormalName:normalImages[i] andSelectName:disabledImages[i] andTitle:tabButtonTitles[i] andIndex:i];
    }
    
    
    ECGTabBarButton *btn = _tabBarView.subviews[_indexTabBarButton];
    
    [self changeViewController:btn]; //自定义的控件中的按钮被点击了调用的方法,默认进入界面就选中第一个按钮
    
//    [[UIApplication sharedApplication].keyWindow addSubview:self.view];
//    [UIApplication sharedApplication].keyWindow.rootViewController = self;
}

#pragma mark 创建一个tab按钮
- (void)creatButtonWithNormalName:(NSString *)normal andSelectName:(NSString *)selected andTitle:(NSString *)title andIndex:(int)index
{
    ECGTabBarButton *button = [ECGTabBarButton buttonWithType:UIButtonTypeCustom];
    button.tag = index;
    
    CGFloat buttonW = _tabBarView.frame.size.width / number;
    CGFloat buttonH = _tabBarView.frame.size.height;
    button.frame = CGRectMake(buttonW *(index), 0, buttonW, buttonH);
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = SystemFont(20);
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    
    [button setImage:[UIImage imageNamed:normal] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:selected] forState:UIControlStateDisabled];//禁用状态
   
    [button setTitle:title forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchDown];
    
    button.imageView.contentMode = UIViewContentModeCenter; // 让图片在按钮内居中
    [button setTitleColor:RGBACOLOR(98, 192, 163, 1) forState:UIControlStateDisabled];
    button.titleLabel.textAlignment = NSTextAlignmentCenter; // 让标题在按钮内居中
    button.titleLabel.font = SystemFont(12);
    
    [_tabBarView addSubview:button];
    if (button.tag == 2) {
        //获取图片大小
        UIImage *im = [UIImage imageNamed:normal];
        CGFloat x = buttonW-(buttonW-im.size.width)/2-2;
        _badgeLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, 0, 10, 10)];
        _badgeLabel.backgroundColor = [UIColor clearColor];
        _badgeLabel.layer.cornerRadius = 5.0f;
        _badgeLabel.layer.masksToBounds = YES;
        [button addSubview:_badgeLabel];
    }
    
}

/**
 *  <#Description#>
 */
- (void)loadVC{
    
    //    ECGChatViewController *chatController = [[ECGChatViewController alloc] initWithNibName:@"ECGChatViewController" bundle:nil];
    for (int j = 0; j<number; j++) {
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controllers[j]];
        [navArray addObject:nav];
    }
    
    self.viewControllers = [NSArray arrayWithArray:navArray];
    
    
}

#pragma mark 按钮被点击时调用
/**
 *  <#Description#>
 *
 *  @param sender <#sender description#>
 */
- (void)changeViewController:(ECGTabBarButton *)sender
{
    self.selectedIndex = sender.tag; //切换不同控制器的界面
    sender.backgroundColor = RGBACOLOR(233, 230, 225, 1.0);
    if (_previousBtn) {
        _previousBtn.backgroundColor = RGBACOLOR(233, 230, 225, 1.0);
    }
    sender.enabled = NO;
    
    if (_previousBtn != sender) {
        
        _previousBtn.enabled = YES;
        
    }
    _previousBtn = sender;
}

//- (void)setSelectedIndex:(NSUInteger)selectedIndex{
//    [super setSelectedIndex:selectedIndex];
//}


/**
 *  <#Description#>
 *
 *  @param b <#b description#>
 */
-(void)hideBottomBar:(BOOL)b {
    //_tabBarView.hidden = b;
    if (b) {
        [_tabBarView removeFromSuperview];
        [bottomView removeFromSuperview];
    }
    else {
        [self.view addSubview:_tabBarView];
        [self.view addSubview:bottomView];
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值