首页 > 代码库 > iOS 自定义tabBarController
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 = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[superviewDidLoad];
// 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 = [[UIViewalloc] initWithFrame:CGRectMake(0, tabBarViewY,320, 49)];
//view 相关设置在这里
[_tabBarView setBackgroundColor:[UIColorgrayColor]];
[self.viewaddSubview:_tabBarView];
for (int i =0; i < 2; i++) {
btn = [[UIButtonalloc] 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) {
[btnsetTitle:@"草帽一"forState:UIControlStateNormal];
[btnsetTitleColor:[UIColorredColor] forState:UIControlStateSelected];
}
if (i==0) {
[btnsetTitle:@"草帽二"forState:UIControlStateNormal];
[btnsetTitleColor:[UIColorredColor] 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;//设置按钮的标记,方便来索引当前的按钮,并跳转到相应的视图
//添加按钮点击事件
[btnaddTarget:selfaction:@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
{
[superdidReceiveMemoryWarning];
// 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
iOS 自定义tabBarController