广告
返回顶部
首页 > 资讯 > 移动开发 >iOS自定义UITabBar中间按钮
  • 225
分享到

iOS自定义UITabBar中间按钮

iOSUITabBar按钮 2022-05-25 01:05:26 225人浏览 独家记忆
摘要

iOS自定义UITabBar中间按钮的具体代码,供大家参考,具体内容如下 自定义YLTbaBar继承自UITabBar git地址 YLTbaBar.h // // YLTab

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

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

--结束END--

本文标题: iOS自定义UITabBar中间按钮

本文链接: https://www.lsjlt.com/news/30442.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • iOS自定义UITabBar中间按钮
    iOS自定义UITabBar中间按钮的具体代码,供大家参考,具体内容如下 自定义YLTbaBar继承自UITabBar git地址 YLTbaBar.h // // YLTab...
    99+
    2022-05-25
    iOS UITabBar 按钮
  • android自定义加减按钮
    本文实例为大家分享了android自定义加减按钮的具体代码,供大家参考,具体内容如下 1、定义两个shape: my_button_shape_normal.xml: <...
    99+
    2022-06-06
    按钮 Android
  • Fastadmin列表自定义按钮
    Fastadmin 列表自定义按钮 FastAdmin是一款基于ThinkPHP+Bootstrap的极速后台开发框架。 文章目录 前言一、单纯的调用接口按钮二、打开新的弹窗页面总结 前...
    99+
    2023-08-31
    php
  • Android如何自定义按钮效果
    安卓原生的按钮是多么丑,效果是多么单调,大家也是有目共睹的。 要做一个APP少不了使用按钮,一个好看的按钮少不了好看的效果和外表,这次主要跟大家讲讲如何用drawable的x...
    99+
    2022-06-06
    自定义 按钮 Android
  • vue自定义封装按钮组件
    vue按钮组件的自定义封装代码,供大家参考,具体内容如下 封装按钮组件 button.vue <template> <button class="butto...
    99+
    2022-11-12
  • Android自定义悬浮按钮效果
    本文实例为大家分享了Android自定义悬浮按钮效果的具体代码,供大家参考,具体内容如下 以下:内容没有参考,写的也是一个比较简单的例子,主要就是应用切换前后台时会显示/隐藏悬浮窗。...
    99+
    2022-11-12
  • FineReport中JS怎么自定义按钮导出
    FineReport中JS怎么自定义按钮导出,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。FineReport支持多种不同的导出方式,直接使用FineReport内置导出按...
    99+
    2023-06-04
  • Android自定义View实现开关按钮
     前言:Android自定义View对于刚入门乃至工作几年的程序员来说都是非常恐惧的,但也是Android进阶学习的必经之路,平时项目中经常会有一些苛刻的需求,我们可...
    99+
    2022-06-06
    view 开关 按钮 Android
  • android自定义按钮示例(重写imagebutton控件实现图片按钮)
    由于项目这种类型的图片按钮比较多,所以重写了ImageButton类。 代码如下:package me.henji.widget; import android.conten...
    99+
    2022-06-06
    示例 图片 按钮 Android
  • C#如何实现自定义圆角按钮
    这篇文章给大家分享的是有关C#如何实现自定义圆角按钮的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。Winform中自带的button没有圆角属性,所以我们继承Button类,重写OnPaint事件来绘制圆角按钮。...
    99+
    2023-06-25
  • Android自定义TimeButton实现倒计时按钮
    项目需要要实现一个带有倒计时功能的按钮,其效果类似发送验证码之后在按钮上显示倒计时并且将按钮设置为不可用的功能。 为了项目中其他地方能够调用到,便重写了一个继承于Button的...
    99+
    2022-06-06
    倒计时 按钮 Android
  • Android自定义实现开关按钮代码
    我们在应用中经常看到一些选择开关状态的配置文件,做项目的时候用的是android的Switch控件,但是感觉好丑的样子子 个人认为还是自定义的比较好,先上个效果图: 实...
    99+
    2022-06-06
    开关 按钮 Android
  • Easyui Datagrid如何实现自定义按钮列
    这篇文章主要介绍Easyui Datagrid如何实现自定义按钮列,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!做项目的时候因为需求,要在表格的最后添加一列操作列,easyUI貌似没...
    99+
    2022-10-19
  • input file自定义按钮美化的方法
    这篇文章主要介绍“input file自定义按钮美化的方法”,在日常操作中,相信很多人在input file自定义按钮美化的方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”...
    99+
    2022-10-19
  • Android自定义开关按钮源码解析
    本文实例为大家分享了Android自定义开关的具体代码,供大家参考,具体内容如下 以 ToggleColorY 为例分析, ToggleImageY逻辑代码差不多 初始化参数 获取背...
    99+
    2022-11-12
  • MotionLayout自定义开关按钮实例详解
    目录MotionLayout自定义一个动画开关按钮使用方法完整代码直接拿去用MotionLayout自定义一个动画开关按钮 MotionLayout是一个非常新的类,它来自Const...
    99+
    2022-11-13
    MotionLayout自定义开关按钮 MotionLayout开关按钮
  • Android怎么实现自定义开关按钮
    这篇文章主要讲解了“Android怎么实现自定义开关按钮”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android怎么实现自定义开关按钮”吧!一、原理我们在界面的某一个区域里放置一个背景图...
    99+
    2023-06-30
  • 如何实现Uniapp中的自定义按钮跳转
    随着移动互联网的发展,移动应用程序开发逐渐成为热门话题。而Uniapp作为一套跨平台开发框架,在移动应用程序的开发中备受欢迎。今天我们将介绍一下Uniapp开发中的自定义按钮跳转功能。Uniapp自带的路由功能可以实现页面之间的跳转,但是如...
    99+
    2023-05-14
  • tkinter模块高级操作(一)—— 透明按钮、透明文本框、自定义按钮及自定义文本框
    【写在前面】 —— 众所周知,tkinter模块中自带的Button类是无法做到使其透明的(至少我无法做到) 【tip:透明是指让背景颜色或图片穿过按钮而显示出来】 —— 找遍了Button类的所有参数和操作,都无法解决这个问题...
    99+
    2023-09-04
    python tkinter gui
  • Android 自定义EditText输入框带清空按钮
    Android 自定义EditText输入框带清空按钮当用户输入字符后 EditText会自动在输入框的内部右侧出现删除按钮重写EditText达到简化布局的效果效果图:继承EditTextpackage com.example.myedi...
    99+
    2023-05-31
    android edittext 清空按钮
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作