文件目录结构如下:
7476A6C9-4148-4E2D-A0BD-9342048A7764.png主要就是上面四个文件(YDInstalTabBar和YDInstalTabBarButton)点击的item,在这使用自定义的button代替,YDInstalTabBar 是继承自UITabBarController, YDInstalTabBarButton继承UIView。
首先在YDInstalTabBarButton里面的操作:
.h文件
@property (nonatomic,retain) UIImage * iconImage;
@property (nonatomic,retain) UIImage * selectedIconImage;
@property (nonatomic,assign) id target;
@property (nonatomic,assign) SEL action;
@property (nonatomic,assign) BOOL selected;
/**
* 按钮的初始化方法(改变图标颜色)
*/
-(id)initWithFrame:(CGRect)frame unSelectedIconImage:(UIImage *)unSelectedIconImage selectedIconImage:(UIImage *)selectedIconImage andTitle:(NSString *)title;
/**
* 按钮的点击事件
*/
-(void)setClickEventTarget:(id)target action:(SEL)action;
.m文件
#import "YDDefine.h"
/**
这是一个消除performSelector警告的宏
*/
#define SuppressPerformSelectorLeakWarning(Stuff)
do {
_Pragma("clang diagnostic push")
_Pragma("clang diagnostic ignored "-Warc-performSelector-leaks"")
Stuff;
_Pragma("clang diagnostic pop")
} while (0)
@implementation YDInstalTabBarButton
-(void)dealloc{
self.iconImage=nil;
self.target=nil;
self.action=nil;
}
-(id)initWithFrame:(CGRect)frame unSelectedIconImage:(UIImage *)unSelectedIconImage selectedIconImage:(UIImage *)selectedIconImage andTitle:(NSString *)title{
if(self=[super initWithFrame:frame]){
self.iconImage=unSelectedIconImage;
self.selectedIconImage =selectedIconImage;
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
imageView.backgroundColor=YDSystemColor;
imageView.userInteractionEnabled=YES;
[self addSubview:imageView];
UITapGestureRecognizer *singleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
[imageView addGestureRecognizer:singleTap];
UIImageView *iconImageView=[[UIImageView alloc]initWithFrame:CGRectMake(12, 15, 30, 25)];
iconImageView.tag=1001;
iconImageView.image=unSelectedIconImage;
[self addSubview:iconImageView];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 48,53, 14)];
label.text=title;
label.tag=1002;
label.font=[UIFont boldSystemFontOfSize:12];
label.textColor=YDSystemTextImageColor;
label.textAlignment=NSTextAlignmentCenter;
[self addSubview:label];
}
return self;
}
-(void)setSelected:(BOOL)selected{
if(_selected!=selected){
_selected=selected;
UIImageView *imageView=(id)[self viewWithTag:1001];
UILabel * label = (id)[self viewWithTag:1002];
if(selected){
imageView.image=self.selectedIconImage;
// label.textColor = YDWhiteColor;
label.textColor = YDselectedTintColor;
}else{
imageView.image=self.iconImage;
label.textColor =YDSystemTextImageColor;
}
}
}
-(void)setClickEventTarget:(id)target action:(SEL)action{
//保存目标和方法
self.target=target;
self.action=action;
}
-(void)singleTap:(UITapGestureRecognizer *)tap{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self.target performSelector:self.action withObject:self];
#pragma clang diagnostic pop}
}
YDInstalTabBar里面的操作稍微复杂一点点:
@interface YDInstalTabBar : UITabBarController
@property (nonatomic,assign) BOOL firstLoad;
@property (nonatomic,retain) UIView * customizedTabbar;
//当选中某个tabBar时,通过此方法刷新界面
-(void)reloadTabBarSelectedByIndex:(NSInteger) index;
在.m文件中引入YDInstalTabBarButton的头文件
@implementation YDInstalTabBar
-(void)dealloc{
self.customizedTabbar=nil;
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSArray *imageArr=@[@"product_N",@"modelRoom_N",@"HomeCollocation_N",@"PlaneLayout_N",@"BrandCulture_N",@"UserCenter_N"];
NSArray *selectedImageArr=@[@"product_NS",@"modelRoom_NS",@"HomeCollocation_NS",@"PlaneLayout_NS",@"BrandCulture_NS",@"UserCenter_NS"];
NSArray *titleArr=@[@"产品中心",@"样板间",@"家居搭配",@"平面布置",@"品牌文化",@"用户中心"];
if(_firstLoad){
NSArray *tabbarBtns=self.tabBar.subviews;
for(UIView *tabbarBtn in tabbarBtns){
tabbarBtn.hidden=YES;
}
for(NSInteger i=0;i<self.viewControllers.count;i++){
YDInstalTabBarButton *BarBtn=[[YDInstalTabBarButton alloc]initWithFrame:CGRectMake(83 + 163*i, 0, 55, 75) unSelectedIconImage:[UIImage imageNamed:imageArr[i]] selectedIconImage:[UIImage imageNamed:selectedImageArr[i]] andTitle:titleArr[i]];
BarBtn.tag=10+i;
[BarBtn setClickEventTarget:self action:@selector(tabbarClick:)];
[self.tabBar addSubview:BarBtn];
}
YDInstalTabBarButton *selectedBtn=(YDInstalTabBarButton *)[self.tabBar viewWithTag:10+self.selectedIndex];
selectedBtn.selected=YES;
_firstLoad=NO;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
_firstLoad=YES;
// Do any additional setup after loading the view.
[[UITabBar appearance] setBarTintColor:YDSystemColor];
//取消tabBar的透明效果
[UITabBar appearance].translucent = NO;
}
- (void)viewWillLayoutSubviews{
CGRect tabFrame =self.tabBar.frame;
tabFrame.size.height= 75;
tabFrame.origin.y= self.view.frame.size.height- 75;
self.tabBar.frame= tabFrame;
}
-(void)tabbarClick:(YDInstalTabBarButton *)button{
YDInstalTabBarButton *currentButton=(YDInstalTabBarButton *)[self.tabBar viewWithTag:(self.selectedIndex+10)];
if(currentButton != button){
currentButton.selected=NO;
button.selected=YES;
self.selectedIndex=button.tag-10;
}
}
-(void)reloadTabBarSelectedByIndex:(NSInteger)index{
YDInstalTabBarButton * currentButton=(YDInstalTabBarButton *)[self.tabBar viewWithTag:(index+15)];
YDInstalTabBarButton * button = (YDInstalTabBarButton *)[self.tabBar viewWithTag:(index+10)];
if(currentButton != button){
currentButton.selected=NO;
button.selected=YES;
self.selectedIndex=button.tag-10;
}
}
-(BOOL)shouldAutorotate{
return NO;
}
网友评论