iOS自定义tabBar

1.创建一个ZQNTabBarButton类继承UIButton

.h中写入

#import <UIKit/UIKit.h>


@interface ZQNTabBarButton : UIButton

@property (nonatomic,strong)UITabBarItem * item;

@end


.m中写入

#import "ZQNTabBarButton.h"

#define ZQNImageRidio 0.7

@implementation ZQNTabBarButton


-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    if (self) {

        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        

        [self setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];

        

        self.imageView.contentMode = UIViewContentModeCenter;

        

        self.titleLabel.textAlignment = NSTextAlignmentCenter;

        

        self.titleLabel.font = [UIFont systemFontOfSize:12];

    }

    return self;

}

-(void)layoutSubviews{

    [super layoutSubviews];

    CGFloat imageX = 0;

    

    CGFloat imageY = 0;

    

    CGFloat imageW = self.bounds.size.width;

    

    CGFloat imageH = self.bounds.size.height * ZQNImageRidio;

    

    self.imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);

    

    CGFloat titleX = 0;

    

    CGFloat titleY = imageH - 3;

    

    CGFloat titleW = self.bounds.size.width;

    

    CGFloat titleH = self.bounds.size.height - titleY;

    

    self.titleLabel.frame = CGRectMake(titleX, titleY, titleW, titleH);

}

-(void)setItem:(UITabBarItem *)item

{

    _item = item;

    //不能漏掉,

    [self observeValueForKeyPath:nil ofObject:nil change:nil context:nil];

    

    [item addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];

    

    [item addObserver:self forKeyPath:@"image" options:NSKeyValueObservingOptionNew context:nil];

    

    [item addObserver:self forKeyPath:@"selectedImage" options:NSKeyValueObservingOptionNew context:nil];

}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{

    

    [self setTitle:_item.title forState:UIControlStateNormal];

    

    [self setImage:_item.image forState:UIControlStateNormal];

    

    [self setImage:_item.selectedImage forState:UIControlStateSelected];

    

}

-(void)dealloc{

    [self.item removeObserver:self forKeyPath:@"title"];

    [self.item removeObserver:self forKeyPath:@"image"];

    [self.item removeObserver:self forKeyPath:@"selectedImage"];

}


2.创建一个ZQNTabBar类继承UIView

.h中写入

#import <UIKit/UIKit.h>

@class ZQNTabBar;

@protocol ZQNTabBarDelegate <NSObject>

//点击item

-(void)tabBar:(ZQNTabBar *)tabBar didClickButton:(NSInteger)index;

//点击中间的大按钮

-(void)didClickCenterButton;


@end

@interface ZQNTabBar : UIView

//接收传进来的tabBarItem

@property(nonatomic, strong)NSArray *items;

@property(nonatomic, assign)id<ZQNTabBarDelegate>delegate;

@end


.m中写入

#import "ZQNTabBar.h"

#import "ZQNTabBarButton.h"

@interface ZQNTabBar()

//按钮的个数

@property(nonatomic, strong)NSMutableArray *buttons;

//选中的按钮

@property(nonatomic, strong)UIButton *selectedButton;

//中间的大按钮

@property(nonatomic, strong)UIButton *plusButton;


@end

@implementation ZQNTabBar

-(UIButton *)plusButton{

    

    if (!_plusButton) {

        

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        [btn setImage:[UIImage imageNamed:@"center"] forState:UIControlStateNormal];

        

        [btn setImage:[UIImage imageNamed:@"centerClick"] forState:UIControlStateHighlighted];

        

        [btn sizeToFit];

        

        [btn addTarget:self action:@selector(didSelected) forControlEvents:UIControlEventTouchUpInside];

        

        _plusButton = btn;

        

        [self addSubview:_plusButton];

        

    }

    return _plusButton;

}


-(NSMutableArray *)buttons{

    

    if (!_buttons) {

        _buttons = [NSMutableArray array];

    }

    

    return _buttons;

    

}

//中间大按钮的点击事件

-(void)didSelected{

    

    if ([self.delegate respondsToSelector:@selector(didClickCenterButton)]) {

        [self.delegate performSelector:@selector(didClickCenterButton)];

    }

    

    

}


//重新布局的时候调用, 调整按钮的位置

- (void)layoutSubviews{

    [super layoutSubviews];

    CGFloat W = self.bounds.size.width;

    

    CGFloat H = self.bounds.size.height;

    

    CGFloat btnX = 0;

    

    CGFloat btnY = 0;

    

    CGFloat btnW = W / (self.items.count + 1);

    

    CGFloat btnH = self.bounds.size.height;

    

    int i = 0;

    

    for (UIView *tabaBarButton in self.buttons) {

        

        if (i == 2) {

            i = 3;

        }

        

        btnX = i * btnW;

        

        tabaBarButton.frame = CGRectMake(btnX, btnY, btnW, btnH);

        i++;

        

    }

    

    self.plusButton.center = CGPointMake(W*0.5, H*0.5-10);

}


-(void)setItems:(NSArray *)items{

    

    _items = items;

    

    for (UITabBarItem *item in _items) {

        

        ZQNTabBarButton *btn = [ZQNTabBarButton buttonWithType:UIButtonTypeCustom];

        

        btn.item = item;

        

        btn.tag = self.buttons.count;

        

        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

        

        if (btn.tag == 0) { // 选中第0

            [self btnClick:btn];

            

        }

        

        [self addSubview:btn];

        

        [self.buttons addObject:btn];

        

    }

    

}

-(void)btnClick:(UIButton *)btn{

    

    _selectedButton.selected = NO;

    

    btn.selected = YES;

    

    _selectedButton = btn;

    

    if ([_delegate respondsToSelector:@selector(tabBar:didClickButton:)]) {

        

        [_delegate tabBar:self didClickButton:btn.tag];

        

    }

    

}


3.创建ZQNNavigationController 继承 UINavigationController

.h中写入

#import <UIKit/UIKit.h>


@interface ZQNNavigationController : UINavigationController


@end


.m中写入

#import "ZQNNavigationController.h"


@interface ZQNNavigationController ()<UIGestureRecognizerDelegate>


@end


@implementation ZQNNavigationController


- (void)viewDidLoad {

    [super viewDidLoad];

//重写UINavigationController侧滑返回手势。(添加navigationItem.leftBarButtonItem时,系统的侧滑返回不可使用,重写后可以)


    UIGestureRecognizer *gesture = self.interactivePopGestureRecognizer;

    NSMutableArray *targets = [gesture valueForKey:@"_targets"];

    id target = [targets firstObject];

    id tar = [target valueForKey:@"target"];

    NSLog(@"%@",tar);

    SEL action = NSSelectorFromString(@"handleNavigationTransition:");

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:tar action:action];

    pan.delegate = self;

    [gesture.view addGestureRecognizer:pan];

    gesture.enabled = NO;


}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{

    if (self.viewControllers.count <= 1) {

        return NO;

    }else{

        return YES;

    }

}

+ (void)initialize{

    UIBarButtonItem *barItem = [UIBarButtonItem appearance];

    NSMutableDictionary *attr = [NSMutableDictionary dictionary];

    attr[NSForegroundColorAttributeName] = [UIColor orangeColor];

    [barItem setTitleTextAttributes:attr forState:UIControlStateNormal];

    NSMutableDictionary *disableAttrs = [NSMutableDictionary dictionary];

    disableAttrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];

    [barItem setTitleTextAttributes:disableAttrs forState:UIControlStateDisabled];

    

}

4.创建HomeViewController继承 UIViewController,MyViewController继承 UIViewController,

AboutViewController继承 UIViewController,CenterViewController继承 UIViewController

  

5.创建ZQNTableViewController继承 UITabBarController

.h中写入


#import <UIKit/UIKit.h>


@interface ZQNTableViewController : UITabBarController


@end

.m中写入

#import "ZQNTableViewController.h"

#import "ZQNNavigationController.h"

#import "ZQNTabBar.h"

#import "CenterViewController.h"


#import "ViewController.h"

#import "HomeViewController.h"

#import "MyViewController.h"

#import "AboutViewController.h"

@interface ZQNTableViewController ()<ZQNTabBarDelegate>

@property(nonatomic, strong)NSMutableArray *items;

@end


@implementation ZQNTableViewController

-(NSMutableArray *)items{

    

    if (!_items) {

        

        _items = [NSMutableArray array];

        

    }

    

    return _items;

    

}


/**

 *  第一次使用这个类的时候调用这个方法

 */

+(void)initialize{

    UITabBarItem *appearance = [UITabBarItem appearance];

    NSDictionary *attDic = @{NSForegroundColorAttributeName:[UIColor orangeColor]};

    [appearance setTitleTextAttributes:attDic forState:UIControlStateSelected];

    NSDictionary *attDicNor = @{NSForegroundColorAttributeName:[UIColor greenColor]};

    [appearance setTitleTextAttributes:attDicNor forState:UIControlStateNormal];

}

- (void)viewDidLoad {

    [super viewDidLoad];

    //初始化所有的子控制器

    [self initChildControllers];

    //添加自定义tabbar

    [self addCustomTabBar];

//    /** 设置系统tabar工具条的背景图片 */

//    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tabbar-light"]];

    

}

/**

 *  添加自定义tabbar

 */

- (void)addCustomTabBar{

    ZQNTabBar *tabBar = [[ZQNTabBar alloc]initWithFrame:self.tabBar.frame];

    

    

    tabBar.backgroundColor = [UIColor whiteColor];

    

    tabBar.delegate = self;

    

    tabBar.items = self.items;

    //添加自定义的tabbar

    [self.view addSubview:tabBar];

    //从试图中移除系统的tabbar

    [self.tabBar removeFromSuperview];

    }


#pragma mark--ZQNTabBarDelegate

//点击item

-(void)tabBar:(ZQNTabBar *)tabBar didClickButton:(NSInteger)index{

    self.selectedIndex = index;

}

//点击中间的大按钮

-(void)didClickCenterButton{

     NSLog(@"点击了中间的按钮");

    CenterViewController *centerVC = [[CenterViewController alloc]init];

    ZQNNavigationController *nav = [[ZQNNavigationController alloc]initWithRootViewController:centerVC];

    [self presentViewController:nav animated:YES completion:nil];

}

-(void)initChildControllers

{

    //添加首页

    HomeViewController *homeVC = [[HomeViewController alloc]init];

    [self addChildViewController:homeVC title:@"首页" imageName:@"tabBar_essence_icon" selectedImageName:@"tabBar_essence_click_icon"];

    //添加我的

    MyViewController *myVC = [[MyViewController alloc]init];

    [self addChildViewController:myVC title:@"我的" imageName:@"tabBar_friendTrends_icon" selectedImageName:@"tabBar_friendTrends_click_icon"];

    //添加关于

    AboutViewController *aboutVC = [[AboutViewController alloc]init];

    [self addChildViewController:aboutVC title:@"历史" imageName:@"tabBar_new_icon"  selectedImageName:@"tabBar_new_click_icon"];

    //添加其他

    ViewController *vc = [[ViewController alloc]init];

    [self addChildViewController:vc title:@"关于" imageName:@"tabBar_me_icon"  selectedImageName:@"tabBar_me_click_icon"];

}

/**

 *  添加一个ChildViewController

 *

 *  @param viewController    子控制器

 *  @param title             标题

 *  @param imageName         图片的名字

 *  @param selectedImageName 选中图片的名字

 */

- (void)addChildViewController:(UIViewController *)viewController title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName{

    UITabBarItem *item = [[UITabBarItem alloc]init];

    item.image = [UIImage imageNamed:imageName];

    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];

    item.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    

    viewController.tabBarItem = item;

    [self.items addObject:viewController.tabBarItem];

    ZQNNavigationController *nav = [[ZQNNavigationController alloc]initWithRootViewController:viewController];

    viewController.title = title;

    

    [self addChildViewController:nav];

}


6.修改AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

    self.window.rootViewController = [[ZQNTableViewController alloc]init];

        //使window可见

    [self.window makeKeyAndVisible];

    return YES;


}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值