自定义tabbar(纯代码)

纯代码写程序可维护性高。

两个类扩展。

UIView+Extension.h

UIView+Extension.h

#import <UIKit/UIKit.h>

@interface UIView (Extension)
@property (nonatomic, assign) CGFloat x;
@property (nonatomic, assign) CGFloat y;
@property (nonatomic, assign) CGFloat centerX;
@property (nonatomic, assign) CGFloat centerY;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) CGSize size;
@property (nonatomic, assign) CGPoint origin;
@end
UIView+Extension.h

#import "UIView+Extension.h"

@implementation UIView (Extension)

- (void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)x
{
    return self.frame.origin.x;
}

- (CGFloat)y
{
    return self.frame.origin.y;
}

- (void)setCenterX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerX
{
    return self.center.x;
}

- (void)setCenterY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (CGFloat)centerY
{
    return self.center.y;
}

- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)height
{
    return self.frame.size.height;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (void)setSize:(CGSize)size
{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGSize)size
{
    return self.frame.size;
}

- (void)setOrigin:(CGPoint)origin
{
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGPoint)origin
{
    return self.frame.origin;
}
@end

UIBarButtonItem+Extension.h

声明UIBarButtonItem+Extension.h

#import <UIKit/UIKit.h>

@interface UIBarButtonItem (Extension)

+ (UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(NSString *)image highImage:(NSString *)highImage;
@end

实现UIBarButtonItem+Extension.m

#import "UIBarButtonItem+Extension.h"
#import "Common.h"
@implementation UIBarButtonItem (Extension)
/**
 *  UIBarButtonItem + Extension
 */
+ (UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(NSString *)image highImage:(NSString *)highImage{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    btn.size = btn.currentBackgroundImage.size;
    
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    return barItem;
}
@end

 #import "AppDelegate.h"初始化窗口

#import "AppDelegate.h"
#import "DSCustomTablebarViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    DSCustomTablebarViewController *tabar = [[DSCustomTablebarViewController alloc] init];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = tabar;
    [self.window makeKeyAndVisible];
    return YES;
}

新建DSCustomTablebarViewController类继承于UIViewController

DSCustomTablebarViewController。h

#import <UIKit/UIKit.h>

@interface DSCustomTablebarViewController : UIViewController

@end

 DSCustomTablebarViewController.m

#import "DSCustomTablebarViewController.h"
#import "DSTabbarItemView.h"
#import "SportsViewController.h"
#import "SleepViewController.h"
#import "StasticViewController.h"
#import "SettingViewController.h"
#import "Common.h"
#define BarItemWidth  MAINVIEWWIDTH/4
#define BarItemHeight 49
@interface DSCustomTablebarViewController ()<DSTabbarItemViewDelegate>
{
    DSTabbarItemView *_sportView;
    DSTabbarItemView *_sleepView;
    DSTabbarItemView *_stasticView;
    DSTabbarItemView *_settingView;
    NSInteger index;
   
}
@property (nonatomic, strong)SportsViewController *sports;
@property (nonatomic, strong)SleepViewController *sleep;
@property (nonatomic, strong)StasticViewController *stastic;
@property (nonatomic, strong)SettingViewController *setting;
@property (nonatomic, strong)NSMutableArray *baritemViewArray;



@end

@implementation DSCustomTablebarViewController
-(NSMutableArray *)baritemViewArray{
    if (!_baritemViewArray) {
        _baritemViewArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _baritemViewArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatTabbar];
    _sports = [[SportsViewController alloc] init];
    _sleep = [[SleepViewController alloc] init];
    _stastic = [[StasticViewController alloc] init];
    _setting = [[SettingViewController alloc] init];
    [self addChildViewController:_sports];
    [self addChildViewController:_sleep];
    [self addChildViewController:_stastic];
    [self addChildViewController:_setting];
    //[self.view addSubview:self.sports.view];
    
}
#pragma mark -- DSTabBarItemViewDelegate
-(void)didSelectedTabbarItem:(DSTabbarItemView *)tabbarItemView{
    index = [self.baritemViewArray indexOfObject:tabbarItemView];
    [self removeItemViewWithIndex:index];
    for (DSTabbarItemView *itemView in self.baritemViewArray) {
        if (itemView == tabbarItemView) {
            itemView.selected = YES;
        }else{
            itemView.selected = NO;
        }
    }
}
-(void)creatTabbar{
    UIView *botomView = [[UIView alloc] init];
    //botomView.backgroundColor = [UIColor purpleColor];
    botomView.frame = CGRectMake(0, MAINVIEWHEIGHT - BarItemHeight, MAINVIEWWIDTH, BarItemHeight);
    _sportView = [DSTabbarItemView creatTabbarWithTitleName:@"运动" WithImage:@"01"];
    _sportView.frame = CGRectMake(0,0, BarItemWidth, BarItemHeight);
    _sportView.delegate = self;
    _sleepView = [DSTabbarItemView creatTabbarWithTitleName:@"睡眠" WithImage:@"02"];
    _sleepView.frame = CGRectMake(BarItemWidth, 0, BarItemWidth, BarItemHeight);
    _sleepView.delegate = self;
    _stasticView = [DSTabbarItemView creatTabbarWithTitleName:@"统计" WithImage:@"03"];
    _stasticView.frame = CGRectMake(BarItemWidth * 2,0, BarItemWidth, BarItemHeight);
    _stasticView.delegate = self;
    _settingView = [DSTabbarItemView creatTabbarWithTitleName:@"设置" WithImage:@"04"];
    _settingView.frame = CGRectMake(BarItemWidth * 3, 0, BarItemWidth, BarItemHeight);
    _settingView.delegate = self;
    [botomView addSubview:_sportView];
    [botomView addSubview:_sleepView];
    [botomView addSubview:_stasticView];
    [botomView addSubview:_settingView];
    [self.view addSubview:botomView];
    [self.baritemViewArray addObjectsFromArray:@[_sportView,_sleepView,_stasticView,_settingView]];
}
-(void)removeItemViewWithIndex:(NSInteger)index{
    switch (index) {
        case 0:
            [self.view addSubview:self.sports.view ];
            [self.sleep.view removeFromSuperview];
            [self.stastic.view removeFromSuperview];
            [self.setting.view removeFromSuperview];
            break;
        case 1:
            [self.view addSubview:self.sleep.view ];
            [self.sports.view removeFromSuperview];
            [self.stastic.view removeFromSuperview];
            [self.setting.view removeFromSuperview];
            break;
        case 2:
            [self.view addSubview:self.stastic.view ];
            [self.sleep.view removeFromSuperview];
            [self.sports.view removeFromSuperview];
            [self.setting.view removeFromSuperview];
            break;
        case 3:
            [self.view addSubview:self.setting.view ];
            [self.sleep.view removeFromSuperview];
            [self.stastic.view removeFromSuperview];
            [self.sports.view removeFromSuperview];
            break;

        default:
            break;
    }
    
}

    

@end

 自定义tabbar继承于UIView

DSTabbarItemView。h

DSTabbarItemView.h

#import <UIKit/UIKit.h>
@class DSTabbarItemView;
@protocol  DSTabbarItemViewDelegate <NSObject>

-(void)didSelectedTabbarItem:(DSTabbarItemView *)tabbarItemView;
@end


@interface DSTabbarItemView : UIView
@property (nonatomic, strong)id<DSTabbarItemViewDelegate>delegate;
@property (nonatomic, assign,getter=isSelected)BOOL selected;
+(DSTabbarItemView *)creatTabbarWithTitleName:(NSString *)title WithImage:(NSString *)image;
@end

DSTabbarItemView.m

#import "DSTabbarItemView.h"
#import "UIView+Extension.h"
@interface DSTabbarItemView()

{
    //小图
    UIImageView *_iconImageView;
    //背景图
    UIImageView *_backgroundImageView;
    //标签
    UILabel *_label;
    
    
}
@end

@implementation DSTabbarItemView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        UIImageView *backgroundImageView = [[UIImageView alloc] init];
        _backgroundImageView = backgroundImageView;
        
        [self addSubview:backgroundImageView];
        UIImageView *iconImageView = [[UIImageView alloc] init];
        _iconImageView = iconImageView;
        [_backgroundImageView addSubview:iconImageView];
        UILabel *lalbel = [[UILabel alloc] init];
        lalbel.textAlignment = NSTextAlignmentCenter;
        lalbel.textColor = [UIColor whiteColor];
        lalbel.font = [UIFont systemFontOfSize:12];
        _label =lalbel;
        [_backgroundImageView addSubview:lalbel];
    }
    return self;
}
-(void)layoutSubviews{
    _backgroundImageView.frame = self.bounds;
    CGFloat iconX = self.width/2 - 13;
    CGFloat iconY = 1;
    CGFloat iconWH = 26;
    _iconImageView.frame = CGRectMake(iconX, iconY, iconWH, iconWH);
    
    CGFloat labelX = 0;
    CGFloat labelY = CGRectGetMaxY(_iconImageView.frame);
    CGFloat labelW = self.width;
    CGFloat labelH = self.height - _iconImageView.height;
    _label.frame = CGRectMake(labelX, labelY, labelW, labelH);
    
}
- (instancetype)initWithTitle:(NSString *)title WithIconImage:(NSString *)iconImage{
    if (self == [super init]) {
        _label.text = title;
        _iconImageView.image = [UIImage imageNamed:iconImage];
        _backgroundImageView.image = [UIImage imageNamed:@"backTab"];
    }
    
    return self;
    
}
+(DSTabbarItemView *)creatTabbarWithTitleName:(NSString *)title WithImage:(NSString *)image{
    return  [[self alloc] initWithTitle:title WithIconImage:image];
}
-(void)setSelected:(BOOL)selected{
    _selected = selected;
    if (self.isSelected == YES) {
        _backgroundImageView.image = [UIImage imageNamed:@"selectedTab"];
    }else{
        _backgroundImageView.image = [UIImage imageNamed:@"backTab"];
    }
}
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{
    [self.delegate didSelectedTabbarItem:self];
}
@end

 另外还需要添加四个控制器,控制器的view的frame需要留出49个像素。

SportsViewController

SleepViewController

StasticViewController

SettingViewController

原文地址:https://www.cnblogs.com/DLS520/p/5011449.html