上一篇讲述了iOS自带的TabBar,但在我所见到的很多App源码中大多用了自己写的TabBar,惯例先上图:
这只是一个最简单的TabBar,但重在原理,虽然是我懒。。。。
添加自定义的TabBar文件
ItemView.h
#import <UIKit/UIKit.h>
//用来定义tabbar按钮大小位置,图片,被选中时的颜色
@interface ItemView :UIControl
{
UIImageView *Itemimageview;
UILabel *Itemlabel;
}
-(void)setItemImage:(UIImage *)image forState:(UIControlState)state;//某种状态下的图片
-(void)setItemTitle:(NSString *)title;//tabbar按钮下的文字
-(void)setItemSelected:(BOOL)isSelected;//当定义的按钮被点击时,告诉被选中了
@end
ItemView.m
#import "ItemView.h"
@implementation ItemView
-(instancetype)initWithFrame:(CGRect)frame{
self=[superinitWithFrame:frame];
if (self) {
CGFloat itemHeight=CGRectGetHeight(frame);
CGFloat itemWidth =CGRectGetWidth(frame);
Itemimageview=[[UIImageViewalloc]initWithFrame:CGRectMake((itemWidth-22)/2,10.f, 22.f,20.f)];
Itemimageview.contentMode=UIViewContentModeScaleAspectFit;
[self addSubview:Itemimageview];
//定义iamgeView的位值大小,以及图片contentMode属性
Itemlabel=[[UILabelalloc]initWithFrame:CGRectMake(0.f, itemHeight-