50、[|50、[ iOS ] UITabBarController 封装使用

50、[|50、[ iOS ] UITabBarController 封装使用
文章图片
效果图
【50、[|50、[ iOS ] UITabBarController 封装使用】1、新建一个继承 UITabBarController 的类,命名 TabBarController,生成 .h 和 .m 文件。

50、[|50、[ iOS ] UITabBarController 封装使用
文章图片
tabbar
2、在 .h 文件中

#import @interface TabBarController : UITabBarController+ (instancetype)instance; @end

3、在 .m 文件中
#import "TabBarController.h" #import "AppDelegate.h"// --四个 tabbarItem 分别对应的视图控制器 #import "MessageViewController.h" #import "DiscoverViewController.h" #import "ContactsViewController.h" #import "MineViewController.h"#define TabBarVC@"vc"//--tabbar对应的视图控制器 #define TabBarTitle@"title"//--tabbar标题 #define TabBarImage@"image"//--未选中时tabbar的图片 #define TabBarSelectedImage@"selectedImage"//--选中时tabbar的图片 #define TabBarItemBadgeValue@"badgeValue"//--未读个数 #define TabBarCount 4//--tabbarItem的个数typedef NS_ENUM(NSInteger,TMTabType) {// --这里的顺序,决定了 tabbar 从左到右item的显示顺序 TMTabTypeMessage,//消息 TMTabTypeDiscover,//发现 TMTabTypeContacts,//联系人 TMTabTypeMine,//我的 }; @interface TabBarController ()@property (nonatomic, strong)NSDictionary *configs; @end@implementation TabBarController+ (instancetype)instance { AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; UIViewController *vc= appDelegate.window.rootViewController; if ([vc isKindOfClass:[TabBarController class]]) {return (TabBarController *)vc; }else {return nil; } }- (void)viewDidLoad { [super viewDidLoad]; [self setUpSubNav]; }- (NSArray*)tabbars{NSMutableArray *items = [[NSMutableArray alloc] init]; for (NSInteger tabbar = 0; tabbar < TabBarCount; tabbar++) { [items addObject:@(tabbar)]; } return items; }- (void)setUpSubNav {NSMutableArray *vcArray = [[NSMutableArray alloc] init]; [self.tabbars enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSDictionary * item =[self vcInfoForTabType:[obj integerValue]]; NSString *vcName = item[TabBarVC]; NSString *title= item[TabBarTitle]; NSString *imageName = item[TabBarImage]; NSString *imageSelected = item[TabBarSelectedImage]; Class clazz = NSClassFromString(vcName); UIViewController *vc = [[clazz alloc] initWithNibName:nil bundle:nil]; vc.hidesBottomBarWhenPushed = NO; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc]; nav.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[UIImage imageNamed:imageName] selectedImage:[[UIImage imageNamed:imageSelected] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]]; nav.tabBarItem.tag = idx; NSInteger badge = [item[TabBarItemBadgeValue] integerValue]; if (badge) { nav.tabBarItem.badgeValue = https://www.it610.com/article/[NSString stringWithFormat:@"%zd",badge]; }[[UITabBar appearance] setTintColor:[UIColor redColor]]; // 设置TabBar上 字体颜色[vcArray addObject:nav]; }]; self.viewControllers = [NSArray arrayWithArray:vcArray]; }#pragma mark - VC - (NSDictionary *)vcInfoForTabType:(TMTabType)type{if (_configs == nil) { _configs = @{ @(TMTabTypeMessage) : @{ TabBarVC: @"MessageViewController", TabBarTitle: @"消息", TabBarImage: @"tabbar_message", TabBarSelectedImage: @"tabbar_messageHL", TabBarItemBadgeValue: @(1) }, @(TMTabTypeDiscover): @{ TabBarVC: @"DiscoverViewController", TabBarTitle: @"发现", TabBarImage: @"tabbar_discover", TabBarSelectedImage: @"tabbar_discoverHL", TabBarItemBadgeValue: @(0)}, @(TMTabTypeContacts): @{ TabBarVC: @"ContactsViewController", TabBarTitle: @"联系人", TabBarImage: @"tabbar_contacts", TabBarSelectedImage: @"tabbar_contactsHL", TabBarItemBadgeValue: @(0)}, @(TMTabTypeMine): @{ TabBarVC: @"MineViewController", TabBarTitle: @"我的", TabBarImage: @"tabbar_mine", TabBarSelectedImage: @"tabbar_mineHL", TabBarItemBadgeValue: @(0)} }; } return _configs[@(type)]; }@end

4、在 AppDelegate 的 didFinishLaunchingWithOptions 方法中调用
TabBarController *tabBarC= [[TabBarController alloc] init]; self.window.rootViewController = tabBarC;

    推荐阅读