作为一个开发新人渣渣,项目基本会用到Tarbar功能,之前用过第三方的,也用过系统原生的。为了满足产品欧巴的要求,我们不得不进行自我改进。话不多说直入主题。
1.需要完成的功能如下图(下面图是盗的 哈哈哈哈(懒得弄图标))
①.
2.实现工能
你需要创建一个自定义的tabBar,继承与UITabBar。(直接贴.h和.m代码吧)
#pragma 自定义TabBar
#import <UIKit/UIKit.h>
@interface LBJTabBar : UITabBar
@property(nonatomic,weak) id<LBJTabBarDelegate>lbjTabBarDelegate;
@end
.m里面的代码
#import "LBJTabBar.h"
#import "UIView+Extension.h"
@interface LBJTabBar ()
@end
@implementation LBJTabBar
-(void)layoutSubviews{
[super layoutSubviews];
CGFloat tabBarButtonW =self.width/5;
CGFloat tabBarButtonIndex = 0;
for (UIView *child in self.subviews) {
Class class = NSClassFromString(@"UITabBarButton");
if ([child isKindOfClass:class]) {
child.x = tabBarButtonIndex *tabBarButtonW;
//设置button的宽度
child.width =tabBarButtonW;
//增加索引
tabBarButtonIndex++;
}
}
}
@end
2.我们还需要创建一个自定义的NavigationController,这是为什么呢?因为我们要设置从tabBar界面push到其他界面的时候要将tabBar隐藏。这时候我们就要在我们自定义NavigationController的重写下面的方法。
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.viewControllers.count > 0) {
// 自动隐藏tabbar
viewController.hidesBottomBarWhenPushed = YES;
// 调用父类pushViewController,self.viewControllers数组添加对象viewController
[super pushViewController:viewController animated:animated];
}
3.创建一个ViewController,继承与UITabBarController(直接上代码)
#import "LBJMainViewController.h"
#import "ViewController.h"
#import "LBJNavionController.h"
#import "LBJTabBar.h"
#import "iCocosSettingViewController.h"
#import "BottomSheetViewController.h"
#import "ScrollHeadViewController.h"
#import "LockViewController.h"
@interface LBJMainViewController ()
@end
@implementation LBJMainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// 添加子控制器
[self addChildVc:[[iCocosSettingViewController alloc] init] title:@"首页" image:@"tabbar_accout@2x.png" selectedImage:@"tabbar_home_selected"];
[self addChildVc:[[LockViewController alloc] init] title:@"新闻" image:@"tabbar_receipt_select@2x.png" selectedImage:@"tabbar_receipt@2x.png"];
[self addChildVc:[[BottomSheetViewController alloc] init] title:@"消息" image:@"tabbar_accout@2x.png" selectedImage:@"tabbar_message_center_selected"];
[self addChildVc:[[ScrollHeadViewController alloc] init] title:@"发现" image:@"tabbar_accout@2x.png" selectedImage:@"tabbar_discover_selected"];
[self addChildVc:[[LockViewController alloc] init] title:@"我的" image:@"tabbar_set_select@2x.png" selectedImage:@"tabbar_profile_selected"];
LBJTabBar *tabBar = [[LBJTabBar alloc] init];
// 修改tabBar为自定义tabBar
[self setValue:tabBar forKey:@"tabBar"];
}
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
// 设置子控制器的文字(可以设置tabBar和navigationBar的文字)
childVc.title = title;
// 设置子控制器的tabBarItem图片
childVc.tabBarItem.image = [UIImage imageNamed:image];
// 禁用图片渲染
childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// 设置文字的样式
[childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blueColor]} forState:UIControlStateNormal];
[childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} forState:UIControlStateSelected];
// 为子控制器包装导航控制器
LBJNavionController *navigationVc = [[LBJNavionController alloc] initWithRootViewController:childVc];
// 添加子控制器
[self addChildViewController:navigationVc];
}
4.到了这里我们就完成了。让我们看看实现的效果。比较low
到这里就完了。技术low借鉴了一些大神的经验。希望能够帮助到一小部分人哈哈。