iOS 封装UITabBarController

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,919评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,567评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,316评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,294评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,318评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,245评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,120评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,964评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,376评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,592评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,764评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,460评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,070评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,697评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,846评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,819评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,665评论 2 354

推荐阅读更多精彩内容