仿闲鱼自定义Tabbar(纯代码)

效果图:

闲鱼TabBar

demo下载地址:https://github.com/jiangcr/XianYuTabBar
Demo中的图片资源均从闲鱼APP中获取,具体获取方法可参考iOS-获取其他APP的图片资源

一、思路

1.UITabBarItem可以看做是一种特殊的button,首先可以创建自定义的button.
2.UITabBar是继承UIView的,可以创建一个自定义的tabbar继承UIView,方便高度自定义。
3.创建自定义的tabBarController继承UITabBarController。
4.通过按钮的点击方法,代理等建立三者的关系。

二、自定义Tabbar的主要过程

1.创建自定义的CustomButton

@interface CustomButton : UIButton
@property(nonatomic, strong)UITabBarItem *tabBarItem;
@end

由于系统的tabBar的图片,标题是通过UITabBarItem的赋值的,这里给CustomButton加个tabBarItem的属性,便于赋值,且更符合思维习惯。

初始化的时候设置image,title的一些公共属性。

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //只需要设置一次的放置在这里
        self.imageView.contentMode = UIViewContentModeBottom;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:12];
        [self setTitleColor:[UIColor colorWithRed:117/255.0f green:117/255.0f blue:117/255.0f alpha:1.0] forState:UIControlStateNormal];   
    }
    return self;
}

通过下面两个方法可以设置image,title的frame信息。

-(CGRect)imageRectForContentRect:(CGRect)contentRect;
-(CGRect)titleRectForContentRect:(CGRect)contentRect;

最后利用tabBarItem的set方法,给button的image,title赋值

- (void)setTabBarItem:(UITabBarItem *)tabBarItem
{
    _tabBarItem = tabBarItem;
    [self setTitle:self.tabBarItem.title forState:UIControlStateNormal];
    [self setImage:self.tabBarItem.image forState:UIControlStateNormal];
    [self setImage:self.tabBarItem.selectedImage forState:UIControlStateSelected];
}

2.创建自定义的CustomTabBar

@interface CustomTabBar : UIView
@property (nonatomic, weak)id <CustomTabBarDelegate>delegate;
- (void)addTabBarButtonWithTabBarItem:(UITabBarItem *)tabBarItem;
@end

这里CustomTabBar有两个作用:
1.CustomButton的一个背景;
2.把CustomButton和tabBarController的viewControllers关联起来

//方法一
- (void)addTabBarButtonWithTabBarItem:(UITabBarItem *)tabBarItem {
    CustomButton * tabBarBtn = [CustomButton new];
    [self addSubview:tabBarBtn];
    tabBarBtn.tabBarItem = tabBarItem;
    [tabBarBtn addTarget:self action:@selector(tabBarBtnClick:) forControlEvents:UIControlEventTouchDown];
    [self.tabbarBtnArray addObject:tabBarBtn];  
    //default selected first one
    if (self.tabbarBtnArray.count == 1) {
        [self tabBarBtnClick:tabBarBtn];
    } 
}

方法一主要就是把CustomButton添加到customTabbar上,CustomButton的数量是与tabBarController的viewControllers有关的,应在创建tabBarController的viewControllers时调用此方法。

要把CustomButton的点击与tabBarController的viewControllers关联起来,需要一个代理方法。在代理方法中直接设置tabBarController的selectedIndex即可

@protocol CustomTabBarDelegate <NSObject>
- (void)tabBar:(CustomTabBar *)tabBar didSelectedButtonFrom:(long)fromBtnTag to:(long)toBtnTag;
@end
#pragma mark --------------------mainTabBar delegate
- (void)tabBar:(CustomTabBar *)tabBar didSelectedButtonFrom:(long)fromBtnTag to:(long)toBtnTag{
    self.selectedIndex = toBtnTag;
}

3.创建自定义的CustomTabBarController

@interface CustomTabBarController : UITabBarController
@end

这部分有两个关键点
1.移除系统的tabBar

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
}

2.viewControllers与CustomButton关联

childVc.tabBarItem.image = [UIImage imageNamed:imageName];
childVc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImageName];
childVc.tabBarItem.title = title;
[self.mainTabBar addTabBarButtonWithTabBarItem:childVc.tabBarItem];
[self addChildViewController:childVc];

以上是自定义tabBar的一个主要过程,下面说一些可能要注意到的问题。

三、自定义tabBar过程中需要注意的问题

1.中间的Button是个特殊的button.

这种情况大多数和闲鱼都比较类似,点击会present一个viewController。这种情况我们可以单独添加一个按钮,单独设置点击方法。调整其frame即可。

- (void)setupPostButton {
    UIButton * postBtn = [UIButton new];
    postBtn.adjustsImageWhenHighlighted = NO;
    [postBtn setBackgroundImage:[UIImage imageNamed:@"post_normal"] forState:UIControlStateNormal];
    [postBtn addTarget:self action:@selector(postGoodAction) forControlEvents:UIControlEventTouchUpInside];
    [postBtn setTitle:@"发布" forState:UIControlStateNormal];
    postBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [postBtn setTitleColor:[UIColor colorWithRed:117/255.0f green:117/255.0f blue:117/255.0f alpha:1.0] forState:UIControlStateNormal];
    postBtn.bounds = CGRectMake(0, 0, postBtn.currentBackgroundImage.size.width, postBtn.currentBackgroundImage.size.height);
    [postBtn setTitleEdgeInsets:UIEdgeInsetsMake(78, 0, 0, 0)];
    [self addSubview:postBtn];
    self.postBtn = postBtn;
}

2.导航栏顶部阴影效果

导航栏顶部原本是有一条线的,并不是阴影效果,达到这种效果有两张方案:
1.加一张阴影效果的image.

self.tabBar.shadowImage = [UIImage imageNamed:@"tapbar_top_line"];

2.代码绘制。
这里讲一下第二种方法

- (void)dropShadowWithOffset:(CGSize)offset
                      radius:(CGFloat)radius
                       color:(UIColor *)color
                     opacity:(CGFloat)opacity {
    
    // Creating shadow path for better performance
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, self.tabBar.bounds);
    self.tabBar.layer.shadowPath = path;
    CGPathCloseSubpath(path);
    CGPathRelease(path);
    
    self.tabBar.layer.shadowColor = color.CGColor;
    self.tabBar.layer.shadowOffset = offset;
    self.tabBar.layer.shadowRadius = radius;
    self.tabBar.layer.shadowOpacity = opacity;
    
    // Default clipsToBounds is YES, will clip off the shadow, so we disable it.
    self.tabBar.clipsToBounds = NO;
}

3.popToRootViewController时tabBar重叠的问题

当项目中出现popToRootViewController回到tabBarController的childViewController时,会出现tabBar重叠的情况,我们之前在tabBarController的viewWillAppear移除系统tabBar此时无效,此时可以通过通知的方法解决此问题。

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeTabBarBtn) name:@"removeTabBarBtn" object:nil];
    
    for (UIView *tabBar in self.tabBar.subviews) {
        if ([tabBar isKindOfClass:[UIControl class]]) {
            [tabBar removeFromSuperview];
        }
    }
}
//返回根视图时移除原有的按钮
- (void)removeTabBarBtn
{
    for (UIView *tabBar in self.tabBar.subviews) {
        if ([tabBar isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabBar removeFromSuperview];
        }
    }
}

** 注意:使用通知记得在合适的时机remove **

有关UITabBarController更详细的内容可参考iOS-UITabBarController详细总结

结束语:

此文是我在学习过程中的探索与总结,不足之处还望大家见谅,并欢迎指正!
愿每一个人都能学有所成!

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

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,631评论 4 59
  • 一、UITabBarController以其相关控件之间的关系 @interface UITabBarContro...
    西门淋雨阅读 2,933评论 0 1
  • 在某些项目的初期我们经常会选择使用UITabbarController或者是UINavigationControl...
    赵永洪阅读 2,972评论 2 1
  • 文/一觉醒来不是梦 近些年,各种视频主播真的像是雨后春笋搬一涌而出。不管是游戏(dota、炉石、魔兽争霸、lol等...
    一觉醒来不是梦阅读 166评论 0 0
  • 起因是道一句晚安啦,你给我发了好长一篇我自以为的小说,还在抱怨好长,耐着脾气给看完,好像有点不解气,还想看,因为...
    Masha迪阅读 180评论 0 0