关于TabBar中间添加一个突出按钮

1.由于老板突然让做个小项目,我擦,那就做吧。
2.然后老板又突然说,你给中间加个这种App的按钮吧,我擦,那好吧。
3.我不知道该如何描述,应该就是五个按钮吧,tabbar.Items[4]。然后最中间那个很突出,点击有一点遮盖效果,下边放图你们瞄一眼就懂了。

根据热心网友的提示,进行了一些简单的修改,我也没有运行查看效果。
另外,给大家分享一个好的,高度自定义的TabBar控件,修改于:2019/1/31 17:12


1.png

2.png

如上边这俩图,大致就是这效果了,点击中间的按钮出现遮盖层,点击遮盖就销毁这个页面。

然后这里说一下,我是百度找的代码,然后修改了一下,这里放一下参考文档


你可能先要把代码复制粘贴下来,那我们就直接上代码,后边说需要用到的补充的东西。

先建一个@interface MyTabBar : UITabBar 不要问我是什么意思。
MyTabbar.h中:

#import <UIKit/UIKit.h>

@class MyTabBar;

//MyTabBar的代理必须实现addButtonClick,以响应中间“+”按钮的点击事件
@protocol MyTabBarDelegate <NSObject>

-(void)addButtonClick:(MyTabBar *)tabBar;

@end

@interface MyTabBar : UITabBar

//指向MyTabBar的代理
@property (nonatomic,weak) id<MyTabBarDelegate> myTabBarDelegate;

@end

MyTabbar.m中:

#import "MyTabbar.h"
#import "UIView+LD.h"

#define AddButtonMargin 10

@interface MyTabBar()

//指向中间“+”按钮
@property (nonatomic,weak) UIButton *addButton;
//指向“添加”标签
@property (nonatomic,weak) UILabel *addLabel;

@end

@implementation MyTabBar

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

-(instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
        {
            //创建中间“+”按钮
            UIButton *addBtn = [[UIButton alloc] init];
            //设置默认背景图片
            [addBtn setBackgroundImage:[UIImage imageNamed:@"app_button01"] forState:UIControlStateNormal];
            //设置按下时背景图片
            [addBtn setBackgroundImage:[UIImage imageNamed:@"app_button01"] forState:UIControlStateHighlighted];
            //添加响应事件
            [addBtn addTarget:self action:@selector(addBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
            //将按钮添加到TabBar
            [self addSubview:addBtn];
            self.addButton = addBtn;
        
            //创建并设置“+”按钮下方的文本为“添加”
            UILabel *addLbl = [[UILabel alloc] init];
            addLbl.text = @"添加";
            addLbl.font = [UIFont systemFontOfSize:11];
            addLbl.textColor = [UIColor grayColor];
            [addLbl sizeToFit];
        
            [self addSubview:addLbl];
            self.addLabel = addLbl;
        }
    return self;
}

//响应中间“+”按钮点击事件
-(void)addBtnDidClick
{
    if([self.myTabBarDelegate respondsToSelector:@selector(addButtonClick:)])
        {
            [self.myTabBarDelegate addButtonClick:self];
        }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    //去掉TabBar上部的横线
    for (UIView *view in self.subviews)
        {
            if ([view isKindOfClass:[UIImageView class]] && view.bounds.size.height <= 1)   //横线的高度为0.5
                {
                    UIImageView *line = (UIImageView *)view;
                    line.hidden = YES;
                }
        }


    //设置“+”按钮的位置
    self.addButton.center = CGPointMake(self.centerX, self.frame.size.height * 0.5 - 1.5 * AddButtonMargin);
    //设置“+”按钮的大小为图片的大小
    self.addButton.size = CGSizeMake(self.addButton.currentBackgroundImage.size.width, self.addButton.currentBackgroundImage.size.height);
    
    //设置“添加”label的位置
    self.addLabel.center = CGPointMake(self.addButton.centerX, CGRectGetMaxY(self.addButton.frame) + 1 * AddButtonMargin);
    
    int btnIndex = 0;
    //系统自带的按钮类型是UITabBarButton,找出这些类型的按钮,然后重新排布位置,空出中间的位置
    Class class = NSClassFromString(@"UITabBarButton");
    for (UIView *btn in self.subviews) {//遍历TabBar的子控件
        if ([btn isKindOfClass:class]) {//如果是系统的UITabBarButton,那么就调整子控件位置,空出中间位置
            //每一个按钮的宽度等于TabBar的五分之一
            btn.width = self.width / 5;
            btn.x = btn.width * btnIndex;
            btnIndex++;
            //如果索引是1(即“+”按钮),直接让索引加一
            if (btnIndex == 2) {
                btnIndex++;
            }
        }
    }
    //将“+”按钮放到视图层次最前面
    [self bringSubviewToFront:self.addButton];
}

//重写hitTest方法,去监听"+"按钮和“添加”标签的点击,目的是为了让凸出的部分点击也有反应
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
   
    //这一个判断是关键,不判断的话push到其他页面,点击“+”按钮的位置也是会有反应的,这样就不好了
    //self.isHidden == NO 说明当前页面是有TabBar的,那么肯定是在根控制器页面
    //在根控制器页面,那么我们就需要判断手指点击的位置是否在“+”按钮或“添加”标签上
    //是的话让“+”按钮自己处理点击事件,不是的话让系统去处理点击事件就可以了
    if (self.isHidden == NO)
    {
           
        //将当前TabBar的触摸点转换坐标系,转换到“+”按钮的身上,生成一个新的点
            CGPoint newA = [self convertPoint:point toView:self.addButton];
            //将当前TabBar的触摸点转换坐标系,转换到“添加”标签的身上,生成一个新的点
            CGPoint newL = [self convertPoint:point toView:self.addLabel];
            
            //判断如果这个新的点是在“+”按钮身上,那么处理点击事件最合适的view就是“+”按钮
            if ( [self.addButton pointInside:newA withEvent:event])
            {
                return self.addButton;
            }
            //判断如果这个新的点是在“添加”标签身上,那么也让“+”按钮处理事件
            else if([self.addLabel pointInside:newL withEvent:event])
            {
                return self.addButton;
            }
            else
            {//如果点不在“+”按钮身上,直接让系统处理就可以了
                     
                return [super hitTest:point withEvent:event];
            }
        }
        else
        {
            //TabBar隐藏了,那么说明已经push到其他的页面了,这个时候还是让系统去判断最合适的view处理就好了
            return [super hitTest:point withEvent:event];
        }
}

@end

上边这个MyTabbar就算是你自定义的一个UITabBar,里边编辑了中间那个突出的按钮。

然后我们还要自定义一个@interface MyTabbarController : UITabBarController
MyTabbarController.h啥都没有
MyTabbarController.m中:

#import "HHTabBarViewController.h"
#import "HHBaseNavigationController.h"
#import "MyTabbar.h"

#import "OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"
#import "FourViewController.h"
#import "HomeView.h"

@interface HHTabBarViewController ()<MyTabBarDelegate> //实现自定义TabBar协议

@end

@implementation HHTabBarViewController

+ (void)initialize {
    
    // 设置UITabBarItem主题
    [self setupTabBarItemTheme];
    
    // 设置UITabBar主题
    [self setupTabBarTheme];
}

+ (void)setupTabBarItemTheme {
    UITabBarItem *tabBarItem = [UITabBarItem appearance];
    
    /**设置文字属性**/
    // 普通状态
    [tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12.0f], NSForegroundColorAttributeName : [UIColor grayColor]} forState:UIControlStateNormal];
    
    // 选中状态
    [tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12.0f],NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];
}

+ (void)setupTabBarTheme {
    
    //    UITabBar *tabBar = [UITabBar appearance];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加所有子控制器
    [self addAllViewControllers];
    
    // 创建自定义TabBar
    [self addCustomTabBar];
}

#pragma mark - 添加所有子控制器
- (void)addAllViewControllers {
    //将需要绑定的页面添加进来
    OneViewController *OneVc = [OneViewController new];
    [self addOneChildVc:OneVc title:@"幼儿园" imageName:@"app_icon02" selectedImageName:@"app_icon02_b"];
    
    TwoViewController *TwoVc = [TwoViewController new];
    [self addOneChildVc:TwoVc title:@"家庭教育" imageName:@"app_icon03" selectedImageName:@"app_icon03_b"];
    
    ThreeViewController *ThreeVc = [ThreeViewController new];
    [self addOneChildVc:ThreeVc title:@"发现" imageName:@"app_icon04" selectedImageName:@"app_icon04_b"];
    
    FourViewController *FourVc = [FourViewController new];
    [self addOneChildVc:FourVc title:@"我" imageName:@"app_icon01" selectedImageName:@"app_icon01_b"];
    
    //创建自定义TabBar
    MyTabBar *myTabBar = [[MyTabBar alloc] init];
    myTabBar.myTabBarDelegate = self;
    
    //利用KVC替换默认的TabBar
    [self setValue:myTabBar forKey:@"tabBar"];
}

#pragma mark - 添加一个子控制器
- (void)addOneChildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)seletedImageName {
    
    childVc.tabBarItem.title = title;
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:seletedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //将NavigationController给包含进来。
    [self addChildViewController:[[HHBaseNavigationController alloc] initWithRootViewController:childVc]];
}

#pragma mark - 自定义TabBar
- (void)addCustomTabBar {
    //    GLTabBar *tabBar = [GLTabBar new];
    //    tabBar.tabBarDelegate = self;
    //    [self setValue:tabBar forKeyPath:@"tabBar"];
}

#pragma mark - MyTabBarDelegate
-(void)addButtonClick:(MyTabBar *)tabBar
{
    //测试中间“+”按钮是否可以点击并处理事件
//    UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"test" message:@"Test" preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *action = [UIAlertAction actionWithTitle:@"test" style:UIAlertActionStyleDefault handler:nil];
//    [controller addAction:action];
//    [self presentViewController:controller animated:YES completion:nil];
    
    HomeView *home = [[HomeView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:home];
}

@end

在项目中我们一般同样需要一个NavigationController,然后与TabBarController关联,可以参考另一片文章——
关于简单的自定义NavigationController和TabBarController

以上内容基本都算完成了,不过你可能问我#import "UIView+LD.h"是什么,这是一个分类,就是自己创建的对UIView的补充,里边有我们常用到的内容,怎么创建请去百度,具体内容,给你个链接看下——iOS开发-很有用的UIView分类

最后你可能还需要一点,就是那个遮盖层,因为没有内容,我就随便胡写了一下,你知道什么意思就行。重点是:它是一个UIView

#import "HomeView.h"

@implementation HomeView

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self initUI];    // 界面
    }
    return self;
}

#pragma mark - 创建UI
-(void)initUI
{
    self.alpha = 0.5;
    self.backgroundColor = [UIColor darkGrayColor];
    [self addTapGesture];
}

#pragma mark - TapGesture
-(void)addTapGesture
{
    // 单击的 Recognizer
    UITapGestureRecognizer* singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTap)];
    //点击的次数
    singleRecognizer.numberOfTapsRequired = 1; // 单击
    
    //给self.view添加一个手势监测;
    [self addGestureRecognizer:singleRecognizer];
}

-(void)SingleTap
{
    if (self) {
        [self removeFromSuperview];
    }
}

@end

以上,希望我没有说错;
有什么问题,请留言;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,907评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,987评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,298评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,586评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,633评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,488评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,275评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,176评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,619评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,819评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,932评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,655评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,265评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,871评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,994评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,095评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,884评论 2 354

推荐阅读更多精彩内容