iOS 自定义tabBarController

//

//  TabBar.m

//  TabBarDemo

//

//  Created by LeeYunHeNB on 14-10-10.

//  Copyright (c) 2014 XinMaHuTong. All rights reserved.

//


#import "TabBar.h"


#import "TabBarBase.h"

@interface TabBar ()

{

    UIButton *btn;

}

@property (nonatomic,strong)UIButton *selectedBtn;

@end


@implementation TabBar


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

}

-(void)viewWillLayoutSubviews

{

    self.tabBarController.tabBar.hidden = NO; //隐藏原先的tabBar

    

    CGFloat tabBarViewY = self.view.frame.size.height - 49;

    UIView *  _tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, tabBarViewY, 320, 49)];

    //view 相关设置在这里

    [_tabBarView setBackgroundColor:[UIColor grayColor]];

    [self.view addSubview:_tabBarView];

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

        btn = [[UIButton alloc] init];

        

        //给按钮添加图片 没有 图片就没加

        //        NSString *imageName = [NSString stringWithFormat:@"TabBar%d", i ];

        //        NSString *imageNameSel = [NSString stringWithFormat:@"TabBar%dSel", i ];

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

        //        [btn setImage:[UIImage imageNamed:imageNameSel] forState:UIControlStateSelected];

        //        [btn setBackgroundColor:[UIColor greenColor]];

        if (i==1) {

            [btn setTitle:@"草帽一" forState:UIControlStateNormal];

            [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];

        }

        

        if (i==0) {

            [btn setTitle:@"草帽二" forState:UIControlStateNormal];

            [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];

        }

        CGFloat x = i * _tabBarView.frame.size.width / 2;

        btn.frame = CGRectMake(x, 0, _tabBarView.frame.size.width / 2, _tabBarView.frame.size.height);

        [_tabBarView addSubview:btn];

        //设置刚进入时,第一个按钮为选中状态

        if (0 == i) {

            btn.selected = YES;

            self.selectedBtn = btn//设置该按钮为选中的按钮

        }

        btn.tag = i;//设置按钮的标记, 方便来索引当前的按钮,并跳转到相应的视图

        //添加 按钮点击事件

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

    }

}

-(void)clickBtn:(UIButton *)sender

{

    if (sender.tag == 0) {

        self.selectedIndex = 0;

    }

    if (sender.tag == 1) {

        self.selectedIndex = 1;

        

    }

    

    

    //1.先将之前选中的按钮设置为未选中

    self.selectedBtn.selected = NO;

    //2.再将当前按钮设置为选中

    

    sender.selected = YES;

    

    //3.最后把当前按钮赋值为之前选中的按钮

    self.selectedBtn = sender;

    

    //4.跳转到相应的视图控制器. (通过selectIndex参数来设置选中了那个控制器)

    

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


//- (void)tabBar:(TabBarBase *)tabBar selectedFrom:(NSInteger)from to:(NSInteger)to {

//    self.selectedIndex = to;

//}

///*

#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.

}



@end

demo下载路径
http://download.csdn.net/detail/u013682799/8020471

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。