封装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<self.tabBarButtons.count;index++){
    
    ZJTabBarButton *button = self.tabBarButtons[index];
    
  CGFloat buttonX = index*buttonW;

    button.frame=CGRectMake(buttonX, buttonY, buttonW, buttonH);
    
    button.tag=index;
    
}
}

四、自定义TabBarButton

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

#import <UIKit/UIKit.h>

@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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,651评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,468评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,931评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,218评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,234评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,198评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,084评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,926评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,341评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,563评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,731评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,430评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,036评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,676评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,829评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,743评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,629评论 2 354

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,080评论 25 707
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,094评论 4 62
  • 这就是我! 我喜欢和直来直去的人相处,有什么就说什么,想什么就聊什么,用不着费尽思考,用不着拐弯抹角,更用不着勾心...
    a3267931bb8f阅读 177评论 0 0
  • 最近‘爱西欧‘又火了,你浮躁了吗? 记不记得8月的时候爱西欧很火,我们每晚准点抢爱西欧的日子? 那段时间ico异常...
    诗人成长阅读 296评论 0 0
  • “子君,开门……”贺涵用手拍打着门。他感觉眼皮很沉很沉,浑身像要散架一样。 无论如何他都要见到子君,他有一肚子话想...
    幸福D苏眉阅读 787评论 0 4