iOS自定义UITabBar中间按钮

 更新时间:2020年6月30日 23:42  点击:1717

iOS自定义UITabBar中间按钮的具体代码,供大家参考,具体内容如下

自定义YLTbaBar继承自UITabBar

git地址

YLTbaBar.h

//
// YLTabBar.h
// 自定义tabbar
//
// Created by nyl on 2018/10/15.
// Copyright © 2018年 nieyinlong. All rights reserved.
//
 
#import <UIKit/UIKit.h>
//tab页面个数
typedef NS_ENUM(NSInteger, kTbaBarItemUIType) {
 kTbaBarItemUIType_Three = 3,//底部3个选项
 kTbaBarItemUIType_Five = 5,//底部5个选项
};
 
@class YLTabBar;
 
@protocol YLTabBarDelegate <NSObject>
 
-(void)tabBar:(YLTabBar *)tabBar clickCenterButton:(UIButton *)sender;
 
@end
 
 
@interface YLTabBar : UITabBar
 
@property (nonatomic, weak) id<YLTabBarDelegate> tabDelegate;
@property (nonatomic, strong) NSString *centerBtnTitle;
@property (nonatomic, strong) NSString *centerBtnIcon;
 
+ (instancetype)instanceCustomTabBarWithType:(kTbaBarItemUIType)type;
 
@end

YLTbaBar.m

//
// YLTabBar.m
// 自定义tabbar
//
// Created by nyl on 2018/10/15.
// Copyright © 2018年 nieyinlong. All rights reserved.
//
 
#import "YLTabBar.h"
 
@interface YLTabBar()
 
@property(nonatomic, strong) UIButton *centerButton;
@property(nonatomic, strong) UILabel *centerTitle;
@property (nonatomic,assign) kTbaBarItemUIType type;
 
@end
 
@implementation YLTabBar
 
+(instancetype)instanceCustomTabBarWithType:(kTbaBarItemUIType)type{
 YLTabBar *tabBar = [[YLTabBar alloc] init];
 tabBar.type = type;
 return tabBar;
}
 
-(instancetype)initWithFrame:(CGRect)frame{
 self = [super initWithFrame:frame];
 if (self) {
 self.translucent = NO;
 UIButton *plusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
 self.centerButton = plusBtn;
 [plusBtn addTarget:self action:@selector(plusBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
 [self addSubview:plusBtn];
 
 UILabel *lblTitle = [[UILabel alloc] init];
 self.centerTitle = lblTitle;
 lblTitle.font = [UIFont systemFontOfSize:10];
 lblTitle.textColor = [UIColor blackColor];
 lblTitle.textAlignment = NSTextAlignmentCenter;
 [self addSubview:lblTitle];
 
 }
 return self;
}
 
-(void)plusBtnDidClick{
 if (self.tabDelegate && [self.tabDelegate respondsToSelector:@selector(tabBar:clickCenterButton:)]) {
 [self.tabDelegate tabBar:self clickCenterButton:self.centerButton];
 }
}
 
 
// 调整子视图的布局
-(void)layoutSubviews{
 [super layoutSubviews];
 CGFloat width = self.frame.size.width/self.type;
 Class class = NSClassFromString(@"UITabBarButton");
 for (UIView *view in self.subviews) {
 if ([view isEqual:self.centerTitle]) {//self.centerButton
  view.frame = CGRectMake(0, 0, width, 15);
  view.center = CGPointMake(self.frame.size.width/2, self.frame.size.height - view.frame.size.height + 8);
 }else if ([view isEqual:self.centerButton]) {//self.centerButton
  view.frame = CGRectMake(0, 0, width, self.frame.size.height);
  [view sizeToFit];
  view.center = CGPointMake(self.frame.size.width/2, 10);
 }else if ([view isKindOfClass:class]){//system button
  CGRect frame = view.frame;
  int indexFromOrign = view.frame.origin.x/width;//防止UIView *view in self.subviews 获取到的不是有序的
  if (indexFromOrign >= (self.type - 1) / 2) {
  indexFromOrign++;
  }
  CGFloat x = indexFromOrign * width;
  //如果是系统的UITabBarButton,那么就调整子控件位置,空出中间位置
  view.frame = CGRectMake(x, view.frame.origin.y, width, frame.size.height);
  
  //调整badge postion
  for (UIView *badgeView in view.subviews){
  NSString *className = NSStringFromClass([badgeView class]);
  // Looking for _UIBadgeView
  if ([className rangeOfString:@"BadgeView"].location != NSNotFound){
   badgeView.layer.transform = CATransform3DIdentity;
   badgeView.layer.transform = CATransform3DMakeTranslation(-17.0, 1.0, 1.0);
   break;
  }
  }
 }
 }
}
 
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
 //这一个判断是关键,不判断的话push到其他页面,点击发布按钮的位置也是会有反应的,这样就不好了
 //self.isHidden == NO 说明当前页面是有tabbar的,那么肯定是在导航控制器的根控制器页面
 //在导航控制器根控制器页面,那么我们就需要判断手指点击的位置是否在发布按钮身上
 //是的话让发布按钮自己处理点击事件,不是的话让系统去处理点击事件就可以了
 if (self.isHidden == NO) {
 //将当前tabbar的触摸点转换坐标系,转换到发布按钮的身上,生成一个新的点
 CGPoint newP = [self convertPoint:point toView:self.centerButton];
 
 //判断如果这个新的点是在发布按钮身上,那么处理点击事件最合适的view就是发布按钮
 if ( [self.centerButton pointInside:newP withEvent:event]) {
  return self.centerButton;
 }else{//如果点不在发布按钮身上,直接让系统处理就可以了
  return [super hitTest:point withEvent:event];
 }
 }
 else {//tabbar隐藏了,那么说明已经push到其他的页面了,这个时候还是让系统去判断最合适的view处理就好了
 return [super hitTest:point withEvent:event];
 }
}
 
-(void)setCenterBtnIcon:(NSString *)centerBtnIcon{
 _centerBtnIcon = centerBtnIcon;
 [self.centerButton setBackgroundImage:[UIImage imageNamed:self.centerBtnIcon] forState:UIControlStateNormal];
 [self.centerButton setBackgroundImage:[UIImage imageNamed:self.centerBtnIcon] forState:UIControlStateHighlighted];
}
 
-(void)setCenterBtnTitle:(NSString *)centerBtnTitle{
 _centerBtnTitle = centerBtnTitle;
 self.centerTitle.text = centerBtnTitle;
}
 
 
@end

在UITabBarController中使用 

// viewDidLoda中, KVO形式添加
[self setValue:self.ylTabBar forKey:@"tabBar"];
 
- (YLTabBar *)ylTabBar {
 if (!_ylTabBar) {
 _ylTabBar = [YLTabBar instanceCustomTabBarWithType:kTbaBarItemUIType_Five];
 _ylTabBar.centerBtnIcon = @"centerIcon";
 _ylTabBar.tabDelegate = self;
 }
 return _ylTabBar;
}

YLTabBarDelegate

-(void)tabBar:(YLTabBar *)tabBar clickCenterButton:(UIButton *)sender{
 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"点击了中间按钮" preferredStyle:UIAlertControllerStyleAlert];
 UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
 
 // TODO
 }];
 [alert addAction:action];
 [self presentViewController:alert animated:YES completion:nil];
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章

  • MySQL性能监控软件Nagios的安装及配置教程

    这篇文章主要介绍了MySQL性能监控软件Nagios的安装及配置教程,这里以CentOS操作系统为环境进行演示,需要的朋友可以参考下...2015-12-14
  • iOS APP h5快捷程序 .mobileconfig的生成

    1.从APP Store 下载Apple Configurator 2从一个管理点管理所有iOS设备应用程序,文档和配置文件。想要确保您的所有家庭成员在其每台iOS设备上都有类似的应用和文档,管理日益增...2021-12-23
  • iOS设置UIButton文字显示位置和字体大小、颜色的方法

    这篇文章给大家分享了iOS如何设置UIButton的文字显示位置和字体的大小、颜色,文中给出了示例代码,相信对大家的学习和理解很有帮助,有需要的朋友们下面来一起看看吧。...2020-06-30
  • iOS如何将图片裁剪成圆形

    这篇文章主要为大家详细介绍了iOS如何将图片裁剪成圆形,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-30
  • 利用JS实现点击按钮后图片自动切换的简单方法

    下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
  • iOS给border设置渐变色的方法实例

    这篇文章主要给大家介绍了关于iOS给border设置渐变色的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-09
  • iOS新版微信底部返回横条问题的解决

    这篇文章主要介绍了iOS新版微信底部返回横条问题的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-30
  • BootStrap栅格系统、表单样式与按钮样式源码解析

    这篇文章主要为大家详细解析了BootStrap栅格系统、表单样式与按钮样式源码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2017-01-23
  • vue+axios全局添加请求头和参数操作

    这篇文章主要介绍了vue+axios全局添加请求头和参数操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-24
  • iOS蓝牙设备名称缓存问题的解决方法

    这篇文章主要给大家介绍了关于iOS蓝牙设备名称缓存问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-08
  • iOS新版微信底部工具栏遮挡问题完美解决

    这篇文章主要介绍了iOS新版微信底部工具栏遮挡问题完美解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-30
  • 封装 axios+promise通用请求函数操作

    这篇文章主要介绍了封装 axios+promise通用请求函数操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-12
  • iOS新增绘制圆的方法实例代码

    这篇文章主要给大家介绍了关于iOS新增绘制圆的方法,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-06-30
  • C#使用浏览按钮获得文件路径和文件夹路径的方法

    这篇文章主要介绍了C#使用浏览按钮获得文件路径和文件夹路径的方法,结合实例形式分析了C#浏览器事件响应及文件操作相关技巧,需要的朋友可以参考下...2020-06-25
  • iOS UIBezierPath实现饼状图

    这篇文章主要为大家详细介绍了iOS UIBezierPath实现饼状图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-20
  • 基于JavaScript实现手机短信按钮倒计时(超简单)

    在淘宝等购物网站,我们都会看到一个发送短信倒计时的按钮,究竟是如何实现的呢?下面小编通过本篇文章给大家分享一段代码关于js实现手机短信按钮倒计时,需要的朋友参考下...2016-01-02
  • python编程PyQt5创建按钮及触发点击事件示例解析

    这篇文章主要为大家介绍了python编程使用PyQt5如何创建按钮及触发点击事件的示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步...2021-10-29
  • IOS获取各种文件目录路径的方法

    ios获取文件路径的方法,iphone沙箱模型的四个文件夹,通过documents,tmp,app,Library得到模拟器路径的简单方式,下面小编整理相关资料,把IOS获取各种文件目录路径的方式总结如下,需要的朋友可以参考下...2020-06-30
  • Vue中 axios delete请求参数操作

    这篇文章主要介绍了Vue中 axios delete请求参数操作,具有很好的参考价值,希望对大家有所 帮助。一起跟随小编过来看看吧...2020-08-26
  • iOS UICollectionView实现卡片效果

    这篇文章主要为大家详细介绍了iOS UICollectionView实现卡片效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-30