码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 自定义tabBarController

时间:2014-10-10 18:54:44      阅读:375      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   io   os   ar   for   strong   

//

//  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

iOS 自定义tabBarController

标签:des   style   http   color   io   os   ar   for   strong   

原文地址:http://blog.csdn.net/u013682799/article/details/39964401

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!