UITabBarController封装

本文介绍了在iOS开发中如何封装UITabBarController,提供了规则和不规则使用方式,包括在.h和.m文件中的实现细节,适用于不同场景的项目需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

没事就封装了一下UITabbarController,项目备用,规则不规则都可以用。

.h文件

#define ScreenWidth ([UIScreen mainScreen].bounds.size.width)
#define ScreenHeight ([UIScreen mainScreen].bounds.size.height)

@interface CommonTabBarController : UITabBarController

@property (nonatomic,strong) UILabel *shadowLineLabel;//不规则tabbar时 可以选择把tabbar ShadowImage 设置透明  自定义一个阴影线
@property (nonatomic,strong) UIButton *middleUpButton;
@property (nonatomic,strong) UIButton *irregularBttuon;

@property (nonatomic,strong) NSArray *tabbarItemSelectedImages;//选中image
@property (nonatomic,strong) NSArray *tabbarItemNormalImages;//未选中image
@property (nonatomic,strong) NSArray *tabbarItemTitles;//tabbar 底部标题
@property (nonatomic,strong) NSArray *containsViewControllers;//tabbarController中显示的ViewController类


/**
 设置tabbarItem的普通文字的颜色和大小、选中文字的颜色和颜色

 @param selectedColor 选中字体的颜色
 @param normalColor 未选中字体的颜色
 @param titleSelectedFontSize 选中字体的大小
 @param titleNormalFontSize 未选中字体的大小
 */
-(void)tabbarItemTitleSelectedColor:(UIColor *)selectedColor  normalColor:(UIColor *)normalColor  titleSelectedFontSize:(float)titleSelectedFontSize  titleNormalFontSize:(float)titleNormalFontSize;

/**
 修改某个底部tabbarItem的字体颜色

 @param item 当前修改的item
 @param normalColor 未选中的颜色
 @param selectedColor 选中的颜色
 */
-(void)changeItemTitleColor:(UITabBarItem *)item normalColor:(UIColor *)normalColor  selectedColor:(UIColor *)selectedColor;

/**
不规则tabbar时  实际上并没有去掉底部阴影线  只是换了一个颜色值
 */
-(void)changeTabbarShadowImage;


/**
 设置不规则按钮

 @param normalImage 未选中的image
 @param selectedImage 选中的image
 @param buttonFrame 不规则按钮的坐标
 @return UIButton
 */
-(UIButton *)irregularBttuonNormalImage:(UIImage *)normalImage selectedImage:(UIImage *)selectedImage  buttonFrame:(CGRect)buttonFrame;
@end

.m文件

#import "CommonTabBarController.h"

@interface CommonTabBarController ()

@end

@implementation CommonTabBarController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)tabbarItemTitleSelectedColor:(UIColor *)selectedColor  normalColor:(UIColor *)normalColor  titleSelectedFontSize:(float)titleSelectedFontSize  titleNormalFontSize:(float)titleNormalFontSize{


    // 设置tabbarItem的普通文字
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] =normalColor;
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:titleNormalFontSize];

    //设置tabBarItem的选中文字颜色
    NSMutableDictionary *selectedTextAttrs = [NSMutableDictionary dictionary];
    selectedTextAttrs[NSForegroundColorAttributeName] = selectedColor;
    selectedTextAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:titleSelectedFontSize];


    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedTextAttrs forState:UIControlStateSelected];

}
/*
  实际上并没有去掉底部阴影线  只是换了一个颜色值
*/
-(void)changeTabbarShadowImage
{
    //去除 TabBar 自带的顶部阴影 位置放这合适
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
    [[UITabBar appearance] setBackgroundImage:    [[UIImage alloc] init]];

}

-(void)changeItemTitleColor:(UITabBarItem *)item normalColor:(UIColor *)normalColor  selectedColor:(UIColor *)selectedColor
{
    //未选中 默认
    [item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:normalColor, NSForegroundColorAttributeName, [UIFont fontWithName:@"Helvetica" size:12.0f],NSFontAttributeName,nil] forState:UIControlStateNormal];

    //被选中
    [item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:selectedColor, NSForegroundColorAttributeName, [UIFont fontWithName:@"Helvetica" size:12.0f],NSFontAttributeName,nil] forState:UIControlStateSelected];

}

-(void)setContainsViewControllers:(NSArray *)containsViewControllers
{
    _containsViewControllers = containsViewControllers;
    NSMutableArray *navigations = [NSMutableArray array];
    for (int i = 0; i<_containsViewControllers.count; i++) {
        UITabBarItem *item = [[UITabBarItem alloc] init];
        item.tag = i+1;
        if (_tabbarItemTitles.count >i) {
            [item setTitle:_tabbarItemTitles[i]];
        }
        if (_tabbarItemNormalImages.count >i) {
            [item setImage:[UIImage imageNamed:_tabbarItemNormalImages[i]]];
        }

        if (_tabbarItemSelectedImages.count >i) {
            [item setSelectedImage:[[UIImage imageNamed:_tabbarItemSelectedImages[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        }

        NSString *className = _containsViewControllers[i];
        UIViewController *subViewController = [[NSClassFromString(className) alloc] init];
        subViewController.tabBarItem = item;
        UINavigationController *vn = [[UINavigationController alloc] initWithRootViewController:subViewController];
        [navigations addObject:vn];
    }
    self.viewControllers = navigations;
}

-(UILabel *)shadowLineLabel
{
    if (!_shadowLineLabel) {
        _shadowLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,-0.5,ScreenWidth,0.5)];
        _shadowLineLabel.backgroundColor = [UIColor lightTextColor];
    }
    return _shadowLineLabel;
}

-(UIButton *)irregularBttuonNormalImage:(UIImage *)normalImage selectedImage:(UIImage *)selectedImage  buttonFrame:(CGRect)buttonFrame
{

    if (!_irregularBttuon) {
        _irregularBttuon = [UIButton buttonWithType:UIButtonTypeCustom];

        [_irregularBttuon setFrame:buttonFrame];
       // [_irregularBttuon setCenter:CGPointMake(self.tabBar.width/2.0,self.tabBar.height/2.0-UIValue(15))];
        [_irregularBttuon setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
        [_irregularBttuon setImage:normalImage forState:UIControlStateNormal];
        [_irregularBttuon setImage:selectedImage forState:UIControlStateSelected];
    }

    return _irregularBttuon;
}
@end

1>新建一个类继承CommonTabBarController

2>规则tabbar只需在viewDidLoad中各个数据赋值即可:

//先走viewDidLoad 再走init
- (void)viewDidLoad {
    [super viewDidLoad];

    self.tabbarItemTitles =@[@"首页",@"消息",@"发现",@"我"];//设置底部tabbar的title  

    self.tabbarItemNormalImages = @[@"tabbar_home",@"tabbar_message_center",@"tabbar_discover",@"tabbar_profile"];//设置底部tabbar未选中状态的图标

    self.tabbarItemSelectedImages = @[@"tabbar_home_highlighted",@"tabbar_message_center_highlighted",@"tabbar_discover_highlighted",@"tabbar_profile_highlighted"];//设置底部tabbar选中状态的图标

    self.containsViewControllers = @[@"MainViewController",@"MessageViewController",@"DiscoveryViewController",@"MineViewController"];//设置tabbar嵌入的Controller类

/*
   tabbar底部字体颜色和大小是默认值,如需修改,可使用此方法设置底部tabbar的字体颜色和字体大小-(void)tabbarItemTitleSelectedColor:(UIColor *)selectedColor  normalColor:(UIColor *)normalColor  titleSelectedFontSize:(float)titleSelectedFontSize  titleNormalFontSize:(float)titleNormalFontSize
*/

}

3>不规则tabbar的使用:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self changeTabbarShadowImage];//设置自带的tabbar阴影线为透明的

   // [self tabbarItemTitleColorByStatus];//设置tabbarItem的title颜色

    self.tabbarItemTitles =@[@"宝宝课程",@"学趣乐园",@"",@"亲子家园",@"我"];//中间为@""是因为项目只需要图标,不需要title,如果你的项目需要显示title的话,可修改为你需要的title

    self.tabbarItemNormalImages = @[@"tabbar_home",@"tabbar_message_center",@"",@"tabbar_discover",@"tabbar_profile"];//中间为@"" 是因为中间的图标需要显示的是自定义的突起button的图标,如果设置图标的话,自定义button图标和item的图标会出现重叠


    self.tabbarItemSelectedImages = @[@"tabbar_home_highlighted",@"tabbar_message_center_highlighted",@"",@"tabbar_discover_highlighted",@"tabbar_profile_highlighted"];


     [self.tabBar addSubview:self.shadowLineLabel];//自定义加个阴影线 避免自带的阴影线遮盖凸起的按钮图标

    [self.tabBar addSubview:[self irregularBttuonNormalImage:[UIImage imageNamed:@"tabbar_home_highlighted"] selectedImage:[UIImage imageNamed:@"tabbar_profile_highlighted"] buttonFrame:CGRectMake((self.tabBar.width-UIValue(40))/2.0,(self.tabBar.height-UIValue(40))/2.0,UIValue(40),UIValue(40))]];//给tabbar添加一个自定义的button  可根据项目需求 进行设置坐标,图标

    [self.irregularBttuon addTarget:self action:@selector(middleUpClick) forControlEvents:UIControlEventTouchUpInside];//给自定义的button添加触发方法    
}

/*
   中间凸起按钮 触发事件
*/
-(void)middleUpClick
{
    MiddleUpViewController *vc = [[MiddleUpViewController alloc] init];//需要模态弹出的Controller类
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    //模态弹出
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:nav animated:YES completion:nil];
}

————————–end ——————————–

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值