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

时间:2023-03-10 07:24:39
IOS开发之功能模块--自定义UITabBarViewController的备用代码

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

 @implementation YMTabBarController

 - (void)viewDidLoad {
[super viewDidLoad];
/**** 初始化一些设置 ****/
[self setUp]; /**** 添加子控制器 ****/
[self addChildViewControllers];
}
- (void)setUp{ } #pragma mark - 在load方法里全局设置所有UITabBarItem的文字属性
// hy:配置全局设置属性
+ (void)load
{
// UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];
// 普通状态下的文字属性
// NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
// normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
// normalAttrs[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"#333333"];
// [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
// 选中状态下的文字属性
// NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
// selectedAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
// selectedAttrs[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"#23ac3a"];
// [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
} #pragma mark - 添加 导航控制器+子控制器
- (void)addChildViewControllers{
// 首页
[self setupOneChildViewController:[[MainViewController alloc] init] title:@"首页" image:@"" selectedImage:@""]; // 我的
[self setupOneChildViewController:[[MineViewController alloc] init] title:@"我的" image:@"" selectedImage:@""];
} #pragma mark - 私有方法
/**
* 初始化一个子控制器
*
* @param vc 子控制器
* @param title 标题
* @param image 图标
* @param selectedImage 选中的图标
*/
- (void)setupOneChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
YMNavigationController *nc = [[YMNavigationController alloc] initWithRootViewController:vc];
// 设置导航控制器的标题
// vc.navigationItem.title = title;
// 设置tabBarItem的表诶
nc.tabBarItem.title = title;
if (image.length) { // 图片名有具体值
nc.tabBarItem.image = [UIImage imageRenderingModeImageNamed:image];
nc.tabBarItem.selectedImage = [UIImage imageRenderingModeImageNamed:selectedImage];
}
[self addChildViewController:nc];
} @end