且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

IOS开发之功能模块--自定义UITabBarViewController的备用代码

更新时间:2022-02-21 22:03:36

前言:因为常用,所以我就备份到这里,然后如果需要修改,可以根据需求进行相关的更改。

IOS开发之功能模块--自定义UITabBarViewController的备用代码
 1 @implementation YMTabBarController
 2  3 - (void)viewDidLoad {
 4  [super viewDidLoad];
 5 /**** 初始化一些设置 ****/  6  [self setUp];
 7  8 /**** 添加子控制器 ****/  9  [self addChildViewControllers];
10 }
11 - (void)setUp{
12 13 }
14 15 #pragma mark - 在load方法里全局设置所有UITabBarItem的文字属性
16 // hy:配置全局设置属性 17 + (void)load
18 {
19 // UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];
20 // 普通状态下的文字属性
21 // NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
22 // normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
23 // normalAttrs[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"#333333"];
24 // [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
25 // 选中状态下的文字属性
26 // NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
27 // selectedAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
28 // selectedAttrs[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"#23ac3a"];
29 // [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected]; 30 }
31 32 #pragma mark - 添加 导航控制器+子控制器
33 - (void)addChildViewControllers{
34 // 首页 35 [self setupOneChildViewController:[[MainViewController alloc] init] title:@"首页" image:@"" selectedImage:@""];
36 37 // 我的 38 [self setupOneChildViewController:[[MineViewController alloc] init] title:@"我的" image:@"" selectedImage:@""];
39 }
40 41 #pragma mark - 私有方法
42 /**
43  * 初始化一个子控制器
44  *
45  * @param vc 子控制器
46  * @param title 标题
47  * @param image 图标
48  * @param selectedImage 选中的图标
49 */ 50 - (void)setupOneChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
51 {
52 YMNavigationController *nc = [[YMNavigationController alloc] initWithRootViewController:vc];
53 // 设置导航控制器的标题
54 // vc.navigationItem.title = title;
55 // 设置tabBarItem的表诶 56 nc.tabBarItem.title = title;
57 if (image.length) { // 图片名有具体值 58 nc.tabBarItem.image = [UIImage imageRenderingModeImageNamed:image];
59 nc.tabBarItem.selectedImage = [UIImage imageRenderingModeImageNamed:selectedImage];
60  }
61  [self addChildViewController:nc];
62 }
63 64 @end
IOS开发之功能模块--自定义UITabBarViewController的备用代码