封装TabBarController

一、封装tabBarController

自己封装了一个底航栏控制器,具体实现步骤如下:
点击下载demo

二、自定义UITabBarController

创建一个继承于UITabBarController的文件。
.m文件实现如下:
1、单例实现初始化

+ (instancetype )shareTabar{
if (!tabar) {
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{

        tabar = [[self alloc]init];
    });
}
return tabar;
}

2、移除交互控件

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];

for(UIView *child in self.tabBar.subviews){
    
    if([child isKindOfClass:[UIControl class]]){
        
        [child removeFromSuperview];//移除自带的tabBar
        
    }
    
}

}
 -(BOOL)shouldAutorotate{
return NO;
}

3、解决重影

-(void)viewWillLayoutSubviews{

[super viewWillLayoutSubviews];

for (UIView *child in self.tabBar.subviews) {
    
    if ([child isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
        
        [child removeFromSuperview];
        
    }
    
}
}

4、初始化ZJTabBar对象

初始化时是把新建的tabBar对象加载在了系统的tabBar上,只是把它给覆盖在了下边,会有重影的可能。

-(void)setupTabBar{

ZJTabBar *customTabBar=[ZJTabBar shareTabBar];

customTabBar.frame=self.tabBar.bounds;

self.customTabBar=customTabBar;

customTabBar.delegate=self;

[self.tabBar addSubview:customTabBar];

}

5、监听按钮的点击

 - (void)tabBar:(ZJTabBar *)tabBar didselectedButtonFrom:(int)from to:(int)to{

self.selectedIndex=to;

  }

6、添加子视图控制器

 -(void)setUPAllChildViewController{

[self.childItemsArray enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL *stop) {
    UIViewController *vc = [NSClassFromString(dict[kClassKey]) new];
    vc.title = dict[kTitleKey];
    ZJNavigationController *nav = [[ZJNavigationController alloc] initWithRootViewController:vc];
    UITabBarItem *item = nav.tabBarItem;
    item.title = dict[kTitleKey];
    item.image = [UIImage imageNamed:dict[kImgKey]];
    item.selectedImage = [[UIImage imageNamed:dict[kSelImgKey]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self addChildViewController:nav];//添加视图控制器
    [self.customTabBar addTabBarButtonWithItem:item];
}];

}

三、自定义TabBar

建一个继承于UIView的ZJTabBar文件
.m文件实现如下:
1、单例初始化对象

+ (id)shareTabBar
{
static ZJTabBar *myTabBar = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
    myTabBar = [[ZJTabBar alloc]init];

});

return myTabBar;
 }

2、添加ZJTabBarButton对象

-(void)addTabBarButtonWithItem:(UITabBarItem *)item{
//1、创建按钮
ZJTabBarButton *button=[[ZJTabBarButton alloc]init];

[self addSubview:button];

//添加按钮到数组中
[self.tabBarButtons addObject:button];

//2、设置数据
button.item=item;

//3、监听按钮点击
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
button.tag = self.tabBarButtons.count - 1;
//4、默认选中第零个
if(self.tabBarButtons.count==1){
    [self buttonClick:button];
}
}

3、监听按钮点击

-(void)buttonClick:(ZJTabBarButton *)button{
if([self.delegate respondsToSelector:@selector(tabBar:didselectedButtonFrom:to:)]){
    [self.delegate tabBar:self didselectedButtonFrom:(int )self.selectedButton.tag to:(int)button.tag];
}
self.selectedButton.selected = NO;
button.selected=YES;
self.selectedButton=button;
}

4、实现layoutSubviews方法,来布局

-(void)layoutSubviews{

[super layoutSubviews];

CGFloat buttonW = self.frame.size.width/self.subviews.count;
    CGFloat buttonH = self.frame.size.height;

CGFloat buttonY = 0;

for(int index = 0;index

四、自定义TabBarButton

新建继承于UIButton的ZJTabBarButton文件。
在.h文件增加属性

#import 

@interface ZJTabBarButton : UIButton

@property(nonatomic,strong)UITabBarItem *item;

@end

在.m实现设置方法。
1、初始化时设置属性

-(id)initWithFrame:(CGRect)frame{

if(self=[super initWithFrame:frame]){
    
    self.imageView.contentMode = UIViewContentModeCenter;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [self setTitleColor:RGBACOLOR(63, 101, 186, 1.0) forState:UIControlStateSelected];//设置选中颜色
    [self setTitleColor:RGBACOLOR(102, 102, 102, 1.0) forState:UIControlStateNormal];

}//选中状态
return self;
}

2、去掉高亮的状态

-(void)setHighlighted:(BOOL)highlighted{}

3、复写两个方法

 -(CGRect)imageRectForContentRect:(CGRect)contentRect{
CGFloat imageW=contentRect.size.width;
 CGFloat imageH=contentRect.size.height*IWTabBarButtonImageRatio;
return CGRectMake(0, 0, imageW , imageH);
}

//内部文字的frame
-(CGRect)titleRectForContentRect:(CGRect)contentRect{
CGFloat titleY = contentRect.size.height*IWTabBarButtonImageRatio;
CGFloat titleW = contentRect.size.width;
CGFloat titleH = contentRect.size.height-titleY;
return CGRectMake(0, titleY, titleW, titleH);
}

4、设置文字和背景图片,实现属性item的set方法

-(void)setItem:(UITabBarItem *)item{
_item = item;
//设置 文字
[self setTitle:self.item.title forState:UIControlStateNormal];
[self setTitle:self.item.title forState:UIControlStateSelected];
//设置图片
[self setImage:self.item.selectedImage forState:UIControlStateSelected];
[self setImage:self.item.image forState:UIControlStateNormal];
}

五、自定义UINavigationController

1、重写Push方法,当push到二级界面后隐藏底导航栏

 -(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{

if (self.viewControllers.count > 0) {
    
    viewController.hidesBottomBarWhenPushed=YES;
    
}
[super pushViewController:viewController animated:YES];
}

2、允许优化返回上一级
系统提供的backbarbuttonitem,不用添加任何代码即可实现向右滑动后退功能,但是往往要对按钮修改样式等时,就需要自定义leftbarbuttonitem,此时向右滑动即失效.通过下面方法即可解决该问题.
主要是通过设置navigationController.interactivePopGestureRecognizer
interactivePopGestureRecognizer属性,这个属性是iOS7以后才有的,自己重新设置interactivePopGestureRecognizer的delegate以让手势继续下去,触发系统的动画action。

self.interactivePopGestureRecognizer.delegate = self;

如果想要右滑的比较顺畅,建议阅读这篇文章:一个丝滑的全屏滑动返回手势。
使用说明:基本使用,把基础类导入到工程中,创建一个继承于ZJTabBarController的类,demo中是MyTabBarController,实现setUPAllChildViewController方法。调回父类是因为在父类中又使用了childItemsArray数组来配置视图控制器。
如下:

-(void)setUPAllChildViewController{

self.childItemsArray = @[
                             @{kClassKey  : @"HomeController",
                               kTitleKey  : @"首页",
                               kImgKey    : @"shouyexuanzhong",
                               kSelImgKey : @"shouye"},
                             
                             @{kClassKey  : @"ServiceController",
                               kTitleKey  : @"服务",
                               kImgKey    : @"fuwu",
                               kSelImgKey : @"fuwuxuanzhong"},
                             
                             @{kClassKey  : @"MeController",
                               kTitleKey  : @"我的",
                               kImgKey    : @"wode",
                               kSelImgKey : @"wodexuanzhong"},
                             ];

[super setUPAllChildViewController];
}

在APPDeledate里设置为根视图

#import "AppDelegate.h"
#import "MyTabBarController.h"
@interface AppDelegate ()
@property(nonatomic,strong)MyTabBarController * zjTabBarController;
@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window                             = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor             = [UIColor clearColor];

self.zjTabBarController = [MyTabBarController shareTabar];
self.window.rootViewController          = self.zjTabBarController;
[self.window makeKeyAndVisible];
return YES;
}

这个是可以直接拿来用的呦。
demo地址:https://github.com/zhengju/TabBarDemo

你可能感兴趣的:(封装TabBarController)