自定义Tabbar

//

//  QYFTabBar.h

//  自定义Tabbar

//

//  Created by ztt on 2017/12/21.

//  Copyright © 2017年 中天科技. All rights reserved.

//


#import <UIKit/UIKit.h>


@class QYFTabBar;

@protocol QYFTabBarDelegate<UITabBarDelegate>

@optional

- (void)tabBarDidClickPlusButton:(QYFTabBar *)tabBar;

@end

@interface QYFTabBar : UITabBar

@property(nonatomic,weak)id<QYFTabBarDelegate>delegate;





#import "QYFTabBar.h"

#import "UIView+Frame.h"

#import "XMGConst.h"

@interface QYFTabBar ()

@property (nonatomic, weak) UIButton *plusButton;


/** 上一次点击的按钮 */

@property (nonatomic, weak) UIControl *previousClickedTabBarButton;


@end


@implementation QYFTabBar

@dynamic delegate;


- (UIButton *)plusButton

{

    if (_plusButton == nil) {

        

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

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

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

        [btn sizeToFit];

        [self addSubview:btn];

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

        _plusButton = btn;

    }

    return _plusButton;

}

/**

 *  加号按钮点击

 */

- (void)plusBtnClick

{

    // 通知代理

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

        [self.delegate tabBarDidClickPlusButton:self];

    }

}

- (void)layoutSubviews

{

    [super layoutSubviews];

    

    // 跳转tabBarButton位置

    NSInteger count = self.items.count;

    

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

    CGFloat btnH = self.xmg_height;

    CGFloat x = 0;

    int i = 0;

    // 私有类:打印出来有个类,但是敲出来没有,说明这个类是系统私有类

    // 遍历子控件 调整布局

    NSLog(@"%lu",(unsigned long)self.subviews.count);

    for (UIControl *tabBarButton in self.subviews) {

        

        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {

            // 设置previousClickedTabBarButton默认值为最前面的按钮

            if (i == 0 && self.previousClickedTabBarButton == nil) {

                self.previousClickedTabBarButton = tabBarButton;

            }

            

            if (i == 2) {

                i += 1;

            }

            

            x = i * btnW;

            

            tabBarButton.frame = CGRectMake(x, 0, btnW, btnH);

            

            i++;

            

            // UIControlEventTouchDownRepeat : 在短时间内连续点击按钮

            

            // 监听点击

            [tabBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];

        }

    }

    

    // 调整发布按钮位置

    self.plusButton.center = CGPointMake(self.xmg_width * 0.5, self.xmg_height * 0.3);

    

}


/**

 *  tabBarButton的点击

 */

- (void)tabBarButtonClick:(UIControl *)tabBarButton

{

    if (self.previousClickedTabBarButton == tabBarButton) {

        // 发出通知,告知外界tabBarButton被重复点击了

        [[NSNotificationCenter defaultCenter] postNotificationName:XMGTabBarButtonDidRepeatClickNotification object:nil];

    }

    

    self.previousClickedTabBarButton = tabBarButton;

}




//

//  QYFTabBarController.h

//  自定义Tabbar

//

//  Created by ztt on 2017/12/21.

//  Copyright © 2017年 中天科技. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface QYFTabBarController : UITabBarController




//

//  QYFTabBarController.m

//  自定义Tabbar

//

//  Created by ztt on 2017/12/21.

//  Copyright © 2017年 中天科技. All rights reserved.

//


#import "QYFTabBarController.h"

#import "UIImage+Image.h"

#import "QYFTabBar.h"

@interface QYFTabBarController ()<QYFTabBarDelegate>


@end


@implementation QYFTabBarController

// 只会调用一次

+ (void)load

{

    // 获取哪个类中UITabBarItem

    UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];

    

    // 设置按钮选中标题的颜色:富文本:描述一个文字颜色,字体,阴影,空心,图文混排

    // 创建一个描述文本属性的字典

    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];

    attrs[NSForegroundColorAttributeName] = [UIColor blackColor];

    [item setTitleTextAttributes:attrs forState:UIControlStateSelected];

    

    // 设置字体尺寸:只有设置正常状态下,才会有效果

    NSMutableDictionary *attrsNor = [NSMutableDictionary dictionary];

    attrsNor[NSFontAttributeName] = [UIFont systemFontOfSize:13];

    [item setTitleTextAttributes:attrsNor forState:UIControlStateNormal];

}

#pragma mark - 生命周期方法

- (void)viewDidLoad {

    [super viewDidLoad];

    

    // Do any additional setup after loading the view.

    // 1 添加子控制器(5个子控制器) -> 自定义控制器 -> 划分项目文件结构

    [self setupAllChildViewController];

    

    // 2 设置tabBar上按钮内容 -> 由对应的子控制器的tabBarItem属性

    [self setupAllTitleButton];

    

    // 3.自定义tabBar

    [self setupTabBar];

}

#pragma mark - 自定义tabBar

- (void)setupTabBar

{

    QYFTabBar *tabBar = [[QYFTabBar alloc] init];

    tabBar.delegate = self;

    [self setValue:tabBar forKey:@"tabBar"];

}

#pragma mark - 添加所有子控制器

- (void)setupAllChildViewController

{

    

    UIViewController *VC1 = [[UIViewController alloc]init];

    VC1.view.backgroundColor = [UIColor redColor];

    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:VC1];

    [self addChildViewController:nav1];

    

    UIViewController *VC2 = [[UIViewController alloc]init];

    VC2.view.backgroundColor = [UIColor yellowColor];

    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:VC2];

    [self addChildViewController:nav2];

    

    UIViewController *VC3 = [[UIViewController alloc]init];

    VC3.view.backgroundColor = [UIColor grayColor];

    UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:VC3];

    [self addChildViewController:nav3];

    

    UIViewController *VC4 = [[UIViewController alloc]init];

    VC4.view.backgroundColor = [UIColor blueColor];

    UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:VC4];

    [self addChildViewController:nav4];


}


// 设置tabBar上所有按钮内容

- (void)setupAllTitleButton

{

   

    UINavigationController *nav = self.childViewControllers[0];

    nav.title = @"tabbar1";

    nav.tabBarItem.image = [UIImage imageNamed:@"icon_sy"];

    // 快速生成一个没有渲染图片

    nav.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"icon_sy_click"];

    

 

    UINavigationController *nav1 = self.childViewControllers[1];

    nav1.title = @"tabbar2";

    nav1.tabBarItem.image = [UIImage imageNamed:@"icon_sz"];

    nav1.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"icon_sz_click"];

    


    UINavigationController *nav2 = self.childViewControllers[2];

    nav2.title = @"tabbar3";

    nav2.tabBarItem.image = [UIImage imageNamed:@"icon_sy"];

    nav2.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"icon_sz_click"];

    

    UINavigationController *nav3 = self.childViewControllers[3];

    nav3.title = @"tabbar4";

    nav3.tabBarItem.image = [UIImage imageNamed:@"icon_sz"];

    nav3.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"icon_sy_click"];


}





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值