一、控制器部分
@end
二、模型部分定义的属性和方法
@property(nonatomic,copy) NSString * title;
/** 图片*/
@property(nonatomic,strong) UIImage * image;//注意uimage使用的是strong,uiimage不是视图
/** 选中时图片*/
@property(nonatomic,strong) UIImage * imageSelected;
- (instancetype)initWithTitle:(NSString *) title andImage:(UIImage *) image;
三、自定义的tabBar部分
@implementation JRTabBar
-(instancetype)initWithFrame:(CGRect)frame andItems:(NSArray *) array{
self = [super initWithFrame:frame];
if (self) {
self.itemArray=array;
[self addItems];
//默认灰色
self.backgroundColor=[UIColor lightGrayColor];
}
return self;
}
#pragma mark - 增加item
- (void)addItems{
CGFloat space =(kWidth-2*kEdgeMargin-self.itemArray.count*40)/(self.itemArray.count-1);
for (int i=0;i<self.itemArray.count;i++) {
JRItem *item= [[[NSBundle mainBundle] loadNibNamed:@"jritem" owner:nil options:nil] firstObject];
item.tag=100+i;
//1 初始化item
JRItemModel * model=self.itemArray[i];
item.imageView.image=model.image;
item.label.text=model.title;
//2 修改坐标
CGRect frame=item.frame;
frame.origin.x=kEdgeMargin+i*(space+40);
item.frame=frame;
//3加入tabbar
[self addSubview:item];
//增加点击手势
UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showInfo:)];
[item addGestureRecognizer:tap];
}
}
- (void) showInfo:(UITapGestureRecognizer *) ges{
NSInteger tag= ges.view.tag;
[self.delegate changeVC:tag];
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/guodongsun0/article/details/46792815