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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章