UITabBar UITabBarItem 自定义

UITabBar 自定义详细解读

不得不说UITabBar神一般的存在

使用过UITabbar的人都知道, 很多App开发都挺喜欢用UITabbar,系统的UITabbar的确很方便,省事,强大的iOS 的api给开发带来很多便利,我们看看企鹅开发的微信就知道了 。。。。 微信的iOS 版就使用了UITabbar,而且可以猜测,八成是用的系统自带的 为啥敢这样下结论,微信不会浪费性能在花哨的UI上的,支付宝的大框架也是UITabbar,还有美团等等 。。。。。
不管是支付宝,微信还是美团 , 他们的UITabbar都有个特点 。。。
这里写图片描述

第一: 每个UITabBarItem 都包含图片和文字 ,而且包含两种状态,选中和未选中
第二:无论选中或者未选中,图片和文字的布局都不变
这种UITabbar是程序猿最喜欢的,因为代码最少,性能最高
But , 还有一种 UITabBarItem 只有图片 , 有代表性的是 Facebook
这里写图片描述

以上两种都比较好办,只有图片的话其实通过移动 系统 UITabBarItem 中图片的位置也可以处理好的 , 但是有些比较 恶心的 UITabBarItem ,观赏下 。。。
这里写图片描述

自定义UITabBarItem

1.通常情况下设计师不懂技术更不懂所谓的性能,模仿的居多,看到别人这样TA也这样;企鹅微信的UI是我见过最简洁且比较容易实现的UI , UITabBarItem为啥微信不自定义了 ? 朋友圈的图片为啥限制在9张之内了 ? 为啥都采用iOS 系统的UITableView了 ? 只能说企鹅的设计师还是有两把刷子的,至少是很懂iOS系统的 。。。。
2.大部分小公司的设计呵呵, 能力没有,怼起来牛逼,你见过高度是 55 的 UITabBar 我就见过,而且语不惊人死不休,来一句“等比放大” 。。。。 无需多吐槽,懂得自然懂。。。

1.首先定义UITabBarItem#####

简单的说,把单个的Item当做一个View来看待就可以 ,图片,文字,大图等 。。。。。
就以我上面截图来说

#import <UIKit/UIKit.h>

@interface HbTabBarItem : UIView{
    UILabel *textLabel;
    UIImage *select,*unselect;
    UIImageView *iconImage;
    UIImageView *bigImage;

}
-(instancetype)initWithTitle:(NSString*)title unselectImage:(NSString * )unsimage selectedImage:(NSString *)selectImg size:(CGSize)imgSize;
-(void)setItemSelected:(BOOL)itemSelected tag:(NSInteger)tag;
@end
#import "HbTabBarItem.h"
#import "Masonry.h"


#define RGBA(R,G,B,A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]
#define RGB(R,G,B) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1.0]


#define HEXRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define HEXRGBA(rgbValue,a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:a]


#define COLOR_666666 HEXRGB(0x666666)
#define COLOR_abaaaa HEXRGB(0xabaaaa)

#define FONT_MEDIUM @"PingFangSC-Medium"
#define FONT_REGULAR @"PingFangSC-Regular"
#define FONT_SEMIBOLD @"PingFangSC-Semibold"
#define FONT_SC @"PingFangSC"


@implementation HbTabBarItem

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)init{
    if(self=[super init]){

    }
    return self;
}

-(void)awakeFromNib{
    [super awakeFromNib];

}

-(void)setup{
    textLabel = [[UILabel alloc] init];
    textLabel.font = [UIFont fontWithName:FONT_REGULAR size:11];
    textLabel.text = @"  ";
    [self addSubview:textLabel];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-4);
        make.centerX.equalTo(self);
    }];
    iconImage = [[UIImageView alloc] init];
    iconImage.image = [UIImage imageNamed:@"bar1_selected"];
    [self addSubview:iconImage];
    [iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(16, 16));
        make.bottom.equalTo(self).offset(-26);
        make.centerX.equalTo(self);
    }];

    bigImage = [[UIImageView alloc] init];
    bigImage.image = [UIImage imageNamed:@"bar1_selectImage"];
    [self addSubview:bigImage];
    [bigImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(42, 42));
        make.center.equalTo(self);
    }];
    bigImage.hidden = YES;
}

-(instancetype)initWithTitle:(NSString*)title unselectImage:(NSString * )unsimage selectedImage:(NSString *)selectImg size:(CGSize)imgSize{
    self=[super init];
    [self setup];


    select = [UIImage imageNamed:selectImg];
    unselect = [UIImage imageNamed: unsimage];
    textLabel.text = title;
    [iconImage setImage:select];
    [iconImage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(imgSize);
        make.bottom.equalTo(self).offset(-24);
        make.centerX.equalTo(self);
    }];
    return self;
}

-(void)setItemSelected:(BOOL)itemSelected tag:(NSInteger)tag{


    if (itemSelected && tag == 0) {
        bigImage.hidden = NO;
        textLabel.hidden = YES;
        iconImage.hidden = YES;
    }else{
        bigImage.hidden = !NO;
        textLabel.hidden = !YES;
        iconImage.hidden = !YES;
    }

    if (itemSelected) {
        iconImage.image = select;
        textLabel.textColor = COLOR_666666;
    }else{
        iconImage.image = unselect;
        textLabel.textColor = COLOR_abaaaa;
    }

}
@end

以上是定义单个的 Item ,然后就想把Item放在一起
也就是自定定义tabbar

#import <UIKit/UIKit.h>

@class HbTabBarItem;

@interface HbTabbar : UITabBar
@property(strong, nonatomic) NSArray <HbTabBarItem*> *tabItems;
-(void)setCurrentIndex:(NSInteger)tag;
@end
#import "HbTabbar.h"
#import "HbTabBarItem.h"


@implementation HbTabbar

-(instancetype)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame])
    {
        self.translucent=NO;

    }
    return self;
}
-(void)clearView{
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}

-(void)setTabItems:(NSArray *)tabItems{
    _tabItems=tabItems;
    [self clearView];
    for (int i=0; i<tabItems.count; i++) {

        HbTabBarItem *barItem=tabItems[i];
        [self addSubview:barItem];
    }

    [self setCurrentIndex:0];
}


-(void)setCurrentIndex:(NSInteger)tag{

    for (int i=0; i<self.tabItems.count; i++) {
        HbTabBarItem *barItem=self.tabItems[i];
        [barItem setItemSelected:i==tag tag:tag];

    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self setupTabBarItems];
}
- (void)setupTabBarItems
{
    CGFloat w = self.frame.size.width;
    CGFloat h = self.frame.size.height;

    int count = (int)self.tabItems.count;
    CGFloat itemY = 0;
    CGFloat itemW = w / count;
    CGFloat itemH = h;

    for (int index = 0; index < count; index++) {
        HbTabBarItem *tabBarItem = self.tabItems[index];
        CGFloat itemX = index * itemW;
        tabBarItem.frame = CGRectMake(itemX, itemY, itemW, itemH);
    }
}

@end

不用多解释 ,复制代码就行 ,
然后就是使用了 。。。。。。

#import "ViewController.h"

#import "HbTabbar.h"
#import "HbTabBarItem.h"

#import "V1ViewController.h"
#import "V2ViewController.h"
#import "V3ViewController.h"
#import "V4ViewController.h"
#define AppFrameWidth  [UIScreen mainScreen].bounds.size.width
#define AppFrameHight [UIScreen mainScreen].bounds.size.height




@interface ViewController ()<UITabBarControllerDelegate>{
    UITabBarController  *viewController;
    HbTabbar *hbTabbar;

}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    V1ViewController *h1 = [[V1ViewController alloc] init];
    HbTabBarItem *t1 = [[HbTabBarItem alloc] initWithTitle:@"首页" unselectImage:@"bar1_unselected" selectedImage:@"bar1_unselected" size:CGSizeMake(18, 16)];


     V2ViewController *h2 = [[V2ViewController alloc] init];
     HbTabBarItem *t2 = [[HbTabBarItem alloc] initWithTitle:@"联系人" unselectImage:@"bar2_unselected" selectedImage:@"bar2_selected" size:CGSizeMake(16, 16)];



     V3ViewController *h3 = [[V3ViewController alloc] init];
    HbTabBarItem *t3 = [[HbTabBarItem alloc] initWithTitle:@"收藏" unselectImage:@"bar3_unselected" selectedImage:@"bar3_selected" size:CGSizeMake(18, 16)];



     V4ViewController *h4 = [[V4ViewController alloc] init];
     HbTabBarItem *t4 = [[HbTabBarItem alloc] initWithTitle:@"其他" unselectImage:@"bar4_unselected" selectedImage:@"bar4_selected" size:CGSizeMake(17, 19)];





    UINavigationController *h11 = [[UINavigationController alloc] initWithRootViewController:h1];
    UINavigationController *h22 = [[UINavigationController alloc] initWithRootViewController:h2];
    UINavigationController *h33 = [[UINavigationController alloc] initWithRootViewController:h3];
    UINavigationController *h44  = [[UINavigationController alloc] initWithRootViewController:h4];


    NSArray *viewControllers = [NSArray arrayWithObjects:h11,h22,h33,h44, nil];


    viewController = [[UITabBarController alloc] init];


    hbTabbar = [[HbTabbar alloc] initWithFrame:viewController.tabBar.frame];
    //    hbTabbar.ydelegate=self;
    [viewController setValue:hbTabbar forKeyPath:@"tabBar"];
    hbTabbar.tabItems=@[t1,t2,t3,t4];

    viewController.delegate = self;
    viewController.viewControllers = viewControllers;
    viewController.view.frame = CGRectMake(0, 0, AppFrameWidth, AppFrameHight);
    [viewController.tabBar setClipsToBounds:YES];
//    [viewController.tabBar insertSubview:backView atIndex:0];
    viewController.tabBar.opaque = YES;
    [self.view addSubview:viewController.view];

}

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

    [hbTabbar setCurrentIndex:tabBarController.selectedIndex];

}

最后,小公司的APP不存在效率问题—因为没人用

源码 下载地址: https://download.csdn.net/download/chmod_r_755/10388492

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值