欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

iOS自定义UITabBar中间按钮

程序员文章站 2023-11-18 21:02:22
ios自定义uitabbar中间按钮的具体代码,供大家参考,具体内容如下自定义yltbabar继承自uitabbargit地址yltbabar.h//// yltabbar.h// 自定义tabbar...

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

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];
}

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