自定义淘宝闲鱼样式tabBar-YLRiseTabBarDemo

前几天微博上几位iOS开发者分享了一个自定义tabBar的demo,淘宝闲鱼app样式,我看了一下,在开发中这一类的自定义tabBar用的还挺多,共有两位作者都写了这个,互相斗法,我觉得各有优劣的,两个demo的地址如下 https://github.com/NoCodeNoWife/LLRiseTabBar-iOS
这个是最原始的,写得比较复杂,整个tabBar上的所有tabBarButtonItem也全是自定义,端的是彻底推翻重来,我觉得大可不必这么复杂,而且这么一来作者将重心全部放在了自定义上,业务逻辑就显得有些乱,他将关于tabBar的方法还有设置控制器等一大堆东西一股脑全部扔在AppDelegate里面,私以为并不是很好。我觉得,在开发中,对于view这一块的东西,controller应该尽可能地知道的少,最好啥也不知道,controller里只有view的初始化和显示,关于view内部的层级结构,视图排布全部放在自定义view里面就好,对于AppDelegate来说就更加重要了,它所做的应该只是初始化window,设置window的根控制器,仅此而已。 【自定义淘宝闲鱼样式tabBar-YLRiseTabBarDemo】--
另一个也是觉得第一个太复杂,直接用了原生的tabBar https://github.com/lianleven/LLRiseTabBar-iOS.git
我看了一下他是在tabBarController里扔了一个空的控制器,这样系统就将tabbar上面的item排布好了,你直接把自己的button盖在上面,这样着实简单,但是搞一个不用的空控制器总觉得怪怪的,可能是因为我是新手,总觉得到处都会有问题,所以不敢做一些冗余的操作,言语总有不及之处,下面且看我的代码。

首先是效果图
自定义淘宝闲鱼样式tabBar-YLRiseTabBarDemo
文章图片
闲鱼 我的思路也是自定义tabbar,但是我在继承UITabBar基础上,添加了一个UIButton,然后在layoutSubviews方法里重新排布位置,达到如图效果,请看代码 YLTabBar.m
-(void)layoutSubviews { [super layoutSubviews]; UIButton *publishButton = [[UIButton alloc] init]; [publishButton setBackgroundImage:[UIImage imageNamed:@"post_normal"] forState:UIControlStateNormal]; [self addSubview:publishButton]; [publishButton addTarget:self action:@selector(publishButtonClick:) forControlEvents:UIControlEventTouchUpInside]; UILabel *label = [[UILabel alloc] init]; label.text = @"发布"; label.font = [UIFont systemFontOfSize:12]; [label sizeToFit]; [self addSubview:label]; //中间button的位置 publishButton.size = publishButton.currentBackgroundImage.size; publishButton.centerX = self.width * 0.5; publishButton.centerY = 0; label.centerX = publishButton.centerX; label.centerY = publishButton.centerY + publishButton.height * 0.7; //设置其他按钮位置 NSUInteger count = self.subviews.count; for (NSUInteger i = 0, j = 0; i < count; i++) { UIView *child = self.subviews[i]; Class class = NSClassFromString(@"UITabBarButton"); if ([child isKindOfClass:class]) { child.width = self.width / 5.0; child.x = self.width * j / 5.0; j++; if (j == 2) {//中间要空出一个位置留给button j++; } } } }

没错就是这么简单,然后我还自定义了tabBarController,将初始化子控制器的一堆代码直接封装在了里面,这样AppDelegate直接设置根控制器就好,不用管这些子控制器怎么加进去的,况且tabBar还需要一个代理,我们当然不能让AppDelegate做tabBar的代理,于是自定义tabBarController非常有必要。 YLTabBarController.m
- (void)viewDidLoad { [super viewDidLoad]; //设置子控制器 YLHomeViewController *home = [[YLHomeViewController alloc] init]; [self setChildVC:home title:@"首页" image:@"home_normal" selectedImage:@"home_highlight"]; YLSameCityViewController *sameCity = [[YLSameCityViewController alloc] init]; [self setChildVC:sameCity title:@"同城" image:@"mycity_normal" selectedImage:@"mycity_highlight"]; YLMessageViewController *message = [[YLMessageViewController alloc] init]; [self setChildVC:message title:@"消息" image:@"message_normal" selectedImage:@"message_highlight"]; YLMineViewController *mine = [[YLMineViewController alloc] init]; [self setChildVC:mine title:@"我的" image:@"account_normal" selectedImage:@"account_highlight"]; YLTabBar *tabBar = [[YLTabBar alloc] init]; tabBar.delegate = self; [self setValue:tabBar forKeyPath:@"tabBar"]; }

没错,你可能已经发现了,我使用KVC方法设置的tabBar,因为UITabBarController的tabBar属性是readonly的,上面作者是将自定义的tabBar直接覆盖在原生的上面,我这里采用的是用KVC替换。好了,就说到这里了,如果你对我的源码有兴趣,github地址是https://github.com/shidayangli/YLRiseTabBarDemo.git。

    推荐阅读