iOS开发自定义tabbar的基本封装

释放双眼,带上耳机,听听看~!

博主因为一直使用同一个框架,而今日跟一个设计师连天,说到切图,关于切图的规范讨论了好久,从设计师的角度他们切的图都是图标边缘开始切,是不会带空白部分的(前提是自己画的图标,从别的地方copy进ps的设计师的图标就算了),聊到这个,博主不禁说有些地方需要他们专门给我留白,甚至切一大块,然后就说到了tabbar,假如我的tabbar有三个item,那么我让设计师切图就是320/3的长,其实是自己偷懒了,包括输入框左视图,我做的时候因为偷懒会需要设计师专门给我把图标拉出来切出一块带空白边框的icon图标,于是博主觉得不能这么懒下去,根据设计师给的图,没有做不出来的工程师,只有偷懒的工程师,所以今天博主就封装了这个tabbar的工程,封装代码如下:

#import <UIKit/UIKit.h>
@interface LHHTabbar : UIView
/*
bgImageName和isUse只能有一个使用另一个必为nil,用来设置整个Tabbar背景色
*/
-(void)creatLHHTabbarWithBackGroundImage:(NSString *)bgImageName orUseBackGroundView:(BOOL)isUse ifUseBGViewWithColor:(UIColor *)bgViewColor withViewControllerArray:(NSArray *)controllerArray withNormalImageArray:(NSArray *)normalImageArray withSelectImageArray:(NSArray *)selectImageArray withTabbarItemTitleArray:(NSArray *)itemTitleArray withTarget:(id)currentTarget withSelector:(SEL)selector;
@end
#import "LHHTabbar.h"
@implementation LHHTabbar
-(void)creatLHHTabbarWithBackGroundImage:(NSString *)bgImageName orUseBackGroundView:(BOOL)isUse ifUseBGViewWithColor:(UIColor *)bgViewColor withViewControllerArray:(NSArray *)controllerArray withNormalImageArray:(NSArray *)normalImageArray withSelectImageArray:(NSArray *)selectImageArray withTabbarItemTitleArray:(NSArray *)itemTitleArray withTarget:(id)currentTarget withSelector:(SEL)selector
{
//创建tabbar背景色,可用图片背景也可用纯色view设置背景
[self creatLHHTabbarWithBackGroundImage:bgImageName orUseBackGroundView:isUse ifUseBGViewWithColor:bgViewColor];
//创建选择器
for (int i=0; i<controllerArray.count; i++) {
[self creatLHHTabbarItemwithViewControllerArray:(NSArray *)controllerArray withNormalImageArray:normalImageArray withSelectImageArray:selectImageArray withTabbarItemTitleArray:(NSArray *)itemTitleArray withIndex:(NSUInteger)i withTarget:currentTarget withSelector:selector];
}
}
-(void)creatLHHTabbarWithBackGroundImage:(NSString *)bgImageName orUseBackGroundView:(BOOL)isUse ifUseBGViewWithColor:(UIColor *)bgViewColor
{
if (bgImageName!=nil) {
UIImageView *imageView=[[UIImageView alloc]initWithFrame:self.bounds];
imageView.image=[UIImage imageNamed:bgImageName];
[self addSubview:imageView];
}
else
{
UILabel *view=[[UILabel alloc]initWithFrame:self.bounds];
view.backgroundColor=bgViewColor;
[self addSubview:view];
}
}
-(void)creatLHHTabbarItemwithViewControllerArray:(NSArray *)controllerArray withNormalImageArray:(NSArray *)normalImageArray withSelectImageArray:(NSArray *)selectImageArray withTabbarItemTitleArray:(NSArray *)itemTitleArray withIndex:(NSUInteger)currentIndex withTarget:(id)currentTarget withSelector:(SEL)selector
{
UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake((self.bounds.size.width/controllerArray.count)*currentIndex, 0, self.bounds.size.width/controllerArray.count, self.bounds.size.height)];
if (currentIndex==0) {
//        bgView.backgroundColor=[UIColor grayColor];
}
else
bgView.backgroundColor=[UIColor clearColor];
[self addSubview:bgView];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
imageView.center=CGPointMake(self.bounds.size.width/controllerArray.count/2, self.bounds.size.height/2-10);
imageView.tag=currentIndex;
imageView.userInteractionEnabled=YES;
if (currentIndex==0) {
imageView.image=[UIImage imageNamed:selectImageArray[currentIndex]];
}
else
imageView.image=[UIImage imageNamed:normalImageArray[currentIndex]];
[bgView addSubview:imageView];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, imageView.center.y+20/2, self.bounds.size.width/controllerArray.count, self.bounds.size.height-(imageView.center.y+20/2))];
label.text=itemTitleArray[currentIndex];
//    label.tag=currentIndex;
if (currentIndex==0) {
label.textColor=[UIColor colorWithRed:0.00f green:0.76f blue:0.83f alpha:1.00f];
}
else
label.textColor=[UIColor whiteColor];
label.textAlignment=NSTextAlignmentCenter;
label.font=[UIFont systemFontOfSize:10];
[bgView addSubview:label];
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(0, 0, bgView.frame.size.width, bgView.frame.size.height);
button.tag=currentIndex;
[button addTarget:currentTarget action:selector forControlEvents:UIControlEventTouchUpInside];
[bgView addSubview:button];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end

当然,封装的方法各式各样,博主这么写也只是觉得方便自己的习惯,像里面的一些字典其实也可以写成一个plist文件来用的,原理都一样,下面把下载地址放在github上了:https://github.com/codeliu6572/ProjectTabbar
还有navigation的封装,博主没有写,其实都很简单,大家用得到的自己封装下。

人已赞赏
iOS文章

iOS开发渐变导航栏

2021-2-3 12:09:52

iOS文章

iOS开发模仿系统方法自定义UIAlertView的实现和代理

2021-2-3 13:24:56

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索