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