电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

iOS自定義UITabBar中間按鈕

瀏覽:2日期:2022-09-17 09:27:17

iOS自定義UITabBar中間按鈕的具體代碼,供大家參考,具體內(nèi)容如下

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頁面?zhèn)€數(shù)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]; }} // 調(diào)整子視圖的布局-(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; //如果是系統(tǒng)的UITabBarButton,那么就調(diào)整子控件位置,空出中間位置 view.frame = CGRectMake(x, view.frame.origin.y, width, frame.size.height); //調(diào)整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{ //這一個判斷是關(guān)鍵,不判斷的話push到其他頁面,點擊發(fā)布按鈕的位置也是會有反應(yīng)的,這樣就不好了 //self.isHidden == NO 說明當前頁面是有tabbar的,那么肯定是在導航控制器的根控制器頁面 //在導航控制器根控制器頁面,那么我們就需要判斷手指點擊的位置是否在發(fā)布按鈕身上 //是的話讓發(fā)布按鈕自己處理點擊事件,不是的話讓系統(tǒng)去處理點擊事件就可以了 if (self.isHidden == NO) { //將當前tabbar的觸摸點轉(zhuǎn)換坐標系,轉(zhuǎn)換到發(fā)布按鈕的身上,生成一個新的點 CGPoint newP = [self convertPoint:point toView:self.centerButton]; //判斷如果這個新的點是在發(fā)布按鈕身上,那么處理點擊事件最合適的view就是發(fā)布按鈕 if ( [self.centerButton pointInside:newP withEvent:event]) { return self.centerButton; }else{//如果點不在發(fā)布按鈕身上,直接讓系統(tǒng)處理就可以了 return [super hitTest:point withEvent:event]; } } else {//tabbar隱藏了,那么說明已經(jīng)push到其他的頁面了,這個時候還是讓系統(tǒng)去判斷最合適的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];}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: IOS
相關(guān)文章:
主站蜘蛛池模板: 骨密度仪-骨密度测定仪-超声骨密度仪-骨龄测定仪-天津开发区圣鸿医疗器械有限公司 | 商秀—企业短视频代运营_抖音企业号托管 | 称重传感器,测力传感器,拉压力传感器,压力变送器,扭矩传感器,南京凯基特电气有限公司 | 展厅设计公司,展厅公司,展厅设计,展厅施工,展厅装修,企业展厅,展馆设计公司-深圳广州展厅设计公司 | 炉门刀边腹板,焦化设备配件,焦化焦炉设备_沧州瑞创机械制造有限公司 | 回转窑-水泥|石灰|冶金-巩义市瑞光金属制品有限责任公司 | 无线对讲-无线对讲系统解决方案-重庆畅博通信 | 合肥钣金加工-安徽激光切割加工-机箱机柜加工厂家-合肥通快 | 发光字|标识设计|标牌制作|精神堡垒 - 江苏苏通广告有限公司 | 订做不锈钢_不锈钢定做加工厂_不锈钢非标定制-重庆侨峰金属加工厂 | 舞台木地板厂家_体育运动木地板_室内篮球馆木地板_实木运动地板厂家_欧氏篮球地板推荐 | 河南档案架,档案密集架,手动密集架,河南密集架批发/报价 | 紫外荧光硫分析仪-硫含量分析仪-红外光度测定仪-泰州美旭仪器 | 特种电缆厂家-硅橡胶耐高温电缆-耐低温补偿导线-安徽万邦特种电缆有限公司 | 防火窗_耐火窗_防火门厂家_防火卷帘门-重庆三乐门业有限公司 | 合肥废气治理设备_安徽除尘设备_工业废气处理设备厂家-盈凯环保 合肥防火门窗/隔断_合肥防火卷帘门厂家_安徽耐火窗_良万消防设备有限公司 | 专注提供国外机电设备及配件-工业控制领域一站式服务商-深圳市华联欧国际贸易有限公司 | lcd条形屏-液晶长条屏-户外广告屏-条形智能显示屏-深圳市条形智能电子有限公司 | 磁力链接搜索神器_BT磁力狗_CILIMAO磁力猫_高效磁力搜索引擎2024 | 硬度计,金相磨抛机_厂家-莱州华煜众信试验仪器有限公司 | 防腐储罐_塑料储罐_PE储罐厂家_淄博富邦滚塑防腐设备科技有限公司 | 餐饮加盟网_特色餐饮连锁加盟店-餐饮加盟官网 | SMC-SMC电磁阀-日本SMC气缸-SMC气动元件展示网 | 智慧钢琴-电钢琴-便携钢琴-数码钢琴-深圳市特伦斯乐器有限公司 | 并网柜,汇流箱,电控设备,中高低压开关柜,电气电力成套设备,PLC控制设备订制厂家,江苏昌伟业新能源科技有限公司 | 飞利浦LED体育场灯具-吸顶式油站灯-飞利浦LED罩棚灯-佛山嘉耀照明有限公司 | 防火板_饰面耐火板价格、厂家_品牌认准格林雅 | 纯水电导率测定仪-万用气体检测仪-低钠测定仪-米沃奇科技(北京)有限公司www.milwaukeeinst.cn 锂辉石检测仪器,水泥成分快速分析仪-湘潭宇科分析仪器有限公司 手术室净化装修-手术室净化工程公司-华锐手术室净化厂家 | 酒吧霸屏软件_酒吧霸屏系统,酒吧微上墙,夜场霸屏软件,酒吧点歌软件,酒吧互动游戏,酒吧大屏幕软件系统下载 | 水性绝缘漆_凡立水_绝缘漆树脂_环保绝缘漆-深圳维特利环保材料有限公司 | 河南卓美创业科技有限公司-河南卓美防雷公司-防雷接地-防雷工程-重庆避雷针-避雷器-防雷检测-避雷带-避雷针-避雷塔、机房防雷、古建筑防雷等-山西防雷公司 | 吲哚菁绿衍生物-酶底物法大肠菌群检测试剂-北京和信同通科技发展有限公司 | 蜗轮丝杆升降机-螺旋升降机-丝杠升降机厂家-润驰传动 | 济南货架定做_仓储货架生产厂_重型货架厂_仓库货架批发_济南启力仓储设备有限公司 | 地磅-电子地磅维修-电子吊秤-汽车衡-无人值守系统-公路治超-鹰牌衡器 | SDG吸附剂,SDG酸气吸附剂,干式酸性气体吸收剂生产厂家,超过20年生产使用经验。 - 富莱尔环保设备公司(原名天津市武清县环保设备厂) | 武汉天安盾电子设备有限公司 - 安盾安检,武汉安检门,武汉安检机,武汉金属探测器,武汉测温安检门,武汉X光行李安检机,武汉防爆罐,武汉车底安全检查,武汉液体探测仪,武汉安检防爆设备 | 成都离婚律师|成都结婚律师|成都离婚财产分割律师|成都律师-成都离婚律师网 | 北京公积金代办/租房发票/租房备案-北京金鼎源公积金提取服务中心 | 电子元器件呆滞料_元器件临期库存清仓尾料_尾料优选现货采购处理交易商城 | 玻璃钢型材-玻璃钢风管-玻璃钢管道,生产厂家-[江苏欧升玻璃钢制造有限公司] |