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;

/**
 *  標(biāo)題常規(guī)顏色
 */
@property (strong, nonatomic) UIColor *titleNormalColor;

/**
 *  標(biāo)題選中顏色
 */
@property (strong, nonatomic) UIColor *titleSelectedColor;

/**
 *  標(biāo)題常規(guī)字體
 */
@property (strong, nonatomic) UIFont *titleNormalFont;

/**
 *  標(biāo)題選中字體
 */
@property (strong, nonatomic) UIFont *titleSelectedFont;

/**
 *  標(biāo)題與圖標(biāo)的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

/**
 *  配置子視圖(僅供子類重寫實現(xiàn)復(fù)雜邏輯用)
 */
- (void)configViewControllers;

/**
 *  增加子視圖(僅供子類重寫實現(xiàn)復(fù)雜邏輯用)
 *
 *  @param childVC 子視圖
 *  @param title 標(biāo)題
 *  @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{
    // 設(shè)置子控制器的文字
    childVC.tabBarItem.title = title;
   
    // 設(shè)置標(biāo)題字體偏移
    [childVC.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0.0, [SFTabBarControllerConfig sharedConfig].yOffset)];
    
    // 設(shè)置選中圖片和普通狀態(tài)圖片
    childVC.tabBarItem.image = [[UIImage imageNamed:normalImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 為子控制器包裝導(dǎo)航控制器
    SFBaseNavigationController *navigationVc = [[SFBaseNavigationController alloc] initWithRootViewController:childVC];
    
    // 添加子控制器
    [self addChildViewController:navigationVc];
}

@end

4.使用

配置相關(guān)參數(shù)

    [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.調(diào)用

    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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容