1.实体类
//
// SFTabBarItem.h
// template
//
// Created by shefeng on 2024/6/26.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface SFTabBarItem : NSObject
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *normalImageName;
@property (strong, nonatomic) NSString *selectedImageName;
@property (strong, nonatomic) NSString *className;
- (instancetype)initWithTitle:(NSString *)title normalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName className:(NSString *)className;
@end
NS_ASSUME_NONNULL_END
//
// SFTabBarItem.m
// template
//
// Created by shefeng on 2024/6/26.
//
#import "SFTabBarItem.h"
@implementation SFTabBarItem
- (instancetype)initWithTitle:(NSString *)title normalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName className:(NSString *)className{
if (self = [super init]) {
self.title = title;
self.normalImageName = normalImageName;
self.selectedImageName = selectedImageName;
self.className = className;
}
return self;
}
@end
2.配置类
//
// SFTabBarControllerConfig.h
// template
//
// Created by shefeng on 2024/6/26.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "SFTabBarItem.h"
NS_ASSUME_NONNULL_BEGIN
@interface SFTabBarControllerConfig : NSObject
/**
* 背景色
*/
@property (strong, nonatomic) UIColor *bgColor;
/**
* 阴影色
*/
@property (strong, nonatomic) UIColor *shadowColor;
/**
* TabBarItem集合
*/
@property (strong, nonatomic) NSArray<SFTabBarItem *> *tabBarItemArr;
/**
* 标题常规颜色
*/
@property (strong, nonatomic) UIColor *titleNormalColor;
/**
* 标题选中颜色
*/
@property (strong, nonatomic) UIColor *titleSelectedColor;
/**
* 标题常规字体
*/
@property (strong, nonatomic) UIFont *titleNormalFont;
/**
* 标题选中字体
*/
@property (strong, nonatomic) UIFont *titleSelectedFont;
/**
* 标题与图标的y轴间距
*/
@property (assign, nonatomic) CGFloat yOffset;
/**
* 单例
*/
+ (instancetype)sharedConfig;
@end
NS_ASSUME_NONNULL_END
//
// SFTabBarControllerConfig.m
// template
//
// Created by shefeng on 2024/6/26.
//
#import "SFTabBarControllerConfig.h"
@implementation SFTabBarControllerConfig
+ (instancetype)sharedConfig{
static dispatch_once_t onceToken;
static SFTabBarControllerConfig *instance;
dispatch_once(&onceToken, ^{
instance = [[SFTabBarControllerConfig alloc] init];
});
return instance;
}
- (UIColor *)bgColor{
if (_bgColor == nil) {
_bgColor = [UIColor whiteColor];
}
return _bgColor;
}
- (UIColor *)shadowColor{
if (_shadowColor == nil) {
_shadowColor = [UIColor clearColor];
}
return _shadowColor;
}
- (UIColor *)titleNormalColor{
if (_titleNormalColor == nil) {
_titleNormalColor = [UIColor blackColor];
}
return _titleNormalColor;
}
- (UIColor *)titleSelectedColor{
if (_titleSelectedColor == nil) {
_titleSelectedColor = [UIColor blackColor];
}
return _titleSelectedColor;
}
- (UIFont *)titleNormalFont{
if (_titleNormalFont == nil) {
_titleNormalFont = [UIFont systemFontOfSize:16];
}
return _titleNormalFont;
}
- (UIFont *)titleSelectedFont{
if (_titleSelectedFont == nil) {
_titleSelectedFont = [UIFont systemFontOfSize:16];
}
return _titleSelectedFont;
}
- (NSArray *)tabBarItemArr{
if (_tabBarItemArr == nil) {
_tabBarItemArr = @[];
}
return _tabBarItemArr;
}
@end
3.基类
//
// SFBaseTabBarController.h
// template
//
// Created by shefeng on 2024/6/26.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface SFBaseTabBarController : UITabBarController
/**
* 配置子视图(仅供子类重写实现复杂逻辑用)
*/
- (void)configViewControllers;
/**
* 增加子视图(仅供子类重写实现复杂逻辑用)
*
* @param childVC 子视图
* @param title 标题
* @param normalImageName 普通图片
* @param selectedImageName 选中图片
*/
- (void)addChildVC:(UIViewController *)childVC title:(NSString *)title normalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName;
@end
NS_ASSUME_NONNULL_END
//
// SFBaseTabBarController.m
// template
//
// Created by shefeng on 2024/6/26.
//
#import "SFBaseTabBarController.h"
#import "SFTabBarControllerConfig.h"
#import "SFBaseNavigationController.h"
#import "UIImage+SFUtil.h"
@interface SFBaseTabBarController ()
@end
@implementation SFBaseTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
[self configViewControllers];
if(@available(iOS 15.0, *)) {
[UITabBar appearance].backgroundColor = [SFTabBarControllerConfig sharedConfig].bgColor;
[UITabBar appearance].backgroundImage = [UIImage createImageWithColor:[SFTabBarControllerConfig sharedConfig].bgColor size:CGSizeMake(1, 1)];
[UITabBar appearance].shadowImage = [UIImage createImageWithColor:[SFTabBarControllerConfig sharedConfig].shadowColor size:CGSizeMake(1, 1)];
}else{
[self.tabBar setBackgroundColor:[SFTabBarControllerConfig sharedConfig].bgColor];
[self.tabBar setBackgroundImage:[UIImage createImageWithColor:[SFTabBarControllerConfig sharedConfig].bgColor size:CGSizeMake(1, 1)]];
[self.tabBar setShadowImage:[UIImage createImageWithColor:[SFTabBarControllerConfig sharedConfig].shadowColor size:CGSizeMake(1, 1)]];
}
UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:@{
NSForegroundColorAttributeName:[SFTabBarControllerConfig sharedConfig].titleNormalColor,
NSFontAttributeName:[SFTabBarControllerConfig sharedConfig].titleNormalFont,
} forState:UIControlStateNormal];
[item setTitleTextAttributes:@{
NSForegroundColorAttributeName:[SFTabBarControllerConfig sharedConfig].titleSelectedColor,
NSFontAttributeName:[SFTabBarControllerConfig sharedConfig].titleSelectedFont,
} forState:UIControlStateSelected];
if (@available(iOS 13.0, *)) {
[UITabBar appearance].tintColor = [SFTabBarControllerConfig sharedConfig].titleSelectedColor;
[UITabBar appearance].unselectedItemTintColor = [SFTabBarControllerConfig sharedConfig].titleNormalColor;
}
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)configViewControllers{
for (SFTabBarItem *item in [SFTabBarControllerConfig sharedConfig].tabBarItemArr) {
[self addChildVC:[[NSClassFromString(item.className) alloc] init] title:item.title normalImageName:item.normalImageName selectedImageName:item.selectedImageName];
}
self.tabBarController.selectedIndex = 0;
}
- (void)addChildVC:(UIViewController *)childVC title:(NSString *)title normalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName{
// 设置子控制器的文字
childVC.tabBarItem.title = title;
// 设置标题字体偏移
[childVC.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0.0, [SFTabBarControllerConfig sharedConfig].yOffset)];
// 设置选中图片和普通状态图片
childVC.tabBarItem.image = [[UIImage imageNamed:normalImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// 为子控制器包装导航控制器
SFBaseNavigationController *navigationVc = [[SFBaseNavigationController alloc] initWithRootViewController:childVC];
// 添加子控制器
[self addChildViewController:navigationVc];
}
@end
4.使用
配置相关参数
[SFTabBarControllerConfig sharedConfig].bgColor = [UIColor whiteColor];
[SFTabBarControllerConfig sharedConfig].shadowColor = [UIColor clearColor];
[SFTabBarControllerConfig sharedConfig].tabBarItemArr = @[
[[SFTabBarItem alloc] initWithTitle:@"首页" normalImageName:@"tabbar_home_normal" selectedImageName:@"tabbar_home_selected" className:@"AViewController"],
[[SFTabBarItem alloc] initWithTitle:@"品书" normalImageName:@"tabbar_publish_normal" selectedImageName:@"tabbar_publish_selected" className:@"BViewController"],
[[SFTabBarItem alloc] initWithTitle:@"" normalImageName:@"tabbar_middle_add" selectedImageName:@"tabbar_middle_add" className:@"UIViewController"],
[[SFTabBarItem alloc] initWithTitle:@"广场" normalImageName:@"tabbar_response_normal" selectedImageName:@"tabbar_response_selected" className:@"CViewController"],
[[SFTabBarItem alloc] initWithTitle:@"我的" normalImageName:@"tabbar_mine_normal" selectedImageName:@"tabbar_mine_selected" className:@"UIViewController"],
];
[SFTabBarControllerConfig sharedConfig].titleNormalFont = [UIFont systemFontOfSize:12];
[SFTabBarControllerConfig sharedConfig].titleSelectedFont = [UIFont systemFontOfSize:12];
[SFTabBarControllerConfig sharedConfig].titleNormalColor = [UIColor grayColor];
[SFTabBarControllerConfig sharedConfig].titleSelectedColor = [UIColor blueColor];
[SFTabBarControllerConfig sharedConfig].yOffset = 2.0f;
5.调用
self.window = [[UIWindow alloc]init];
self.window.frame = [UIScreen mainScreen].bounds;
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = [[SFBaseTabBarController alloc] init];
[self.window makeKeyAndVisible];
6.附工具类
//
// UIImage+SFUtil.h
// template
//
// Created by shefeng on 2024/6/26.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIImage (SFUtil)
/**
* color2image
*
* @param color 颜色
* @param size 大小
* @return 纯色的图片
*/
+ (UIImage *)createImageWithColor:(UIColor *)color size:(CGSize)size;
@end
NS_ASSUME_NONNULL_END
//
// UIImage+SFUtil.m
// template
//
// Created by shefeng on 2024/6/26.
//
#import "UIImage+SFUtil.h"
@implementation UIImage (SFUtil)
/**
* color2image
*
* @param color 颜色
* @param size 大小
* @return 纯色的图片
*/
+ (UIImage *)createImageWithColor:(UIColor *)color size:(CGSize)size {
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end