50、[|50、[ iOS ] UITabBarController 封装使用
文章图片
效果图
【50、[|50、[ iOS ] UITabBarController 封装使用】1、新建一个继承 UITabBarController 的类,命名 TabBarController,生成 .h 和 .m 文件。
文章图片
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;
推荐阅读
- 一个人的碎碎念
- 野营记-第五章|野营记-第五章 讨伐梦魇兽
- Shell-Bash变量与运算符
- 清明,是追思、是传承、是感恩。
- 牛人进化+|牛人进化+ 按自己的意愿过一生
- 七老修复好敏感、角质层薄、红血丝
- 华为旁!大社区、地铁新盘,佳兆业城市广场五期!
- 2020-04-07vue中Axios的封装和API接口的管理
- 标签、语法规范、内联框架、超链接、CSS的编写位置、CSS语法、开发工具、块和内联、常用选择器、后代元素选择器、伪类、伪元素。
- 螃蟹和这些食物同吃,轻则腹泻、重则中毒!要小心哦~