服务器之家

服务器之家 > 正文

iOS自定义UITabBar中间按钮

时间:2021-06-03 15:22     来源/作者:--风起云涌--

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

iOS自定义UITabBar中间按钮

自定义yltbabar继承自uitabbar

git地址

yltbabar.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//
// 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// 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中使用 

?
1
2
3
4
5
6
7
8
9
10
11
// 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

?
1
2
3
4
5
6
7
8
9
-(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];
}

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

原文链接:https://blog.csdn.net/NLYNN/article/details/83537630

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部