自定义标签栏(重用性高的方法)

2024-05-28 09:58

本文主要是介绍自定义标签栏(重用性高的方法),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

- (void)addItemWithIcon:(NSString *)icon title:(NSString *)title {//1.初始化CustomTabBarItem *item = [[CustomTabBarItem alloc]init];[item setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];[item setTitle:title forState:UIControlStateNormal];[item addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];//2.添加子视图[self addSubview:item];//3.调整item的frame[self itemFrame];
}
//很好地一个方法
- (void)itemFrame {NSInteger count = self.subviews.count;CGFloat height = CGRectGetHeight(self.bounds);CGFloat width = CGRectGetWidth(self.bounds) / count;for (int i = 0; i < count; i ++) {CustomTabBarItem *tabBarItem = self.subviews[i];tabBarItem.tag = i; //绑定标记tabBarItem.frame = CGRectMake(width * i, 0, width, height);}
}
自定义标签View (

CustomTabBarItem (按钮)

)

可重用性高,

设置点击事件监听的代理

@protocol CustomTabBarDlegate <NSObject>
@optional
- (void)customTabBar:(CustomTabBarView *)tabBar itemSelectedFrom:(NSInteger)from to:(NSInteger)to;@end

@property (nonatomic,weak)id<CustomTabBarDlegate> delegate;

- (void)buttonPressed:(CustomTabBarItem *)item {//通知代理:if ([_delegate respondsToSelector:@selector(customTabBar:itemSelectedFrom:to:)]) {[_delegate customTabBar:self itemSelectedFrom:_selectedItem.tag to:item.tag];}_selectedItem = item;
}

在外面使用:(要增加标签直接再写add方法就行了)

 //1.添加 CustomTabBarViewcustomTabBarView = [[CustomTabBarView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds) - CUSTOMTABBARVIEW_HEIGHT,CGRectGetWidth(self.view.bounds), CUSTOMTABBARVIEW_HEIGHT)];customTabBarView.delegate = self;[self.view addSubview:customTabBarView];//2.往 CustomTabBarView 里添加内容[customTabBarView addItemWithIcon:@"商品" title:@"xiangu"];[customTabBarView addItemWithIcon:@"消息" title:@"xiangu"];[customTabBarView addItemWithIcon:@"订单" title:@"xiangu"];//3.添加其他控制器[self customTabBar:customTabBarView itemSelectedFrom:0 to:0];


切换视图控制器
#pragma mark - CustomTabBarView的代理方法
- (void)customTabBar:(CustomTabBarView *)tabBar itemSelectedFrom:(NSInteger)from to:(NSInteger)to {NSLog(@"%ld,%ld",from,to);[_currentVC.view removeFromSuperview];[_currentVC removeFromParentViewController];if (to == 0) {UIViewController *vc = [[UIViewController alloc]init];self.view.backgroundColor = [UIColor greenColor];[self.view addSubview:vc.view];[self addChildViewController:vc];_currentVC = vc;}else if (to == 1){UIViewController *vc = [[UIViewController alloc]init];vc.view.backgroundColor = [UIColor redColor];[self.view addSubview:vc.view];[self addChildViewController:vc];_currentVC = vc;}else {UIViewController *vc = [[UIViewController alloc]init];vc.view.backgroundColor = [UIColor orangeColor];[self.view addSubview:vc.view];[self addChildViewController:vc];_currentVC = vc;}[self.view bringSubviewToFront:customTabBarView];}



这篇关于自定义标签栏(重用性高的方法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1010243

相关文章

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

Maven 配置中的 <mirror>绕过 HTTP 阻断机制的方法

《Maven配置中的<mirror>绕过HTTP阻断机制的方法》:本文主要介绍Maven配置中的<mirror>绕过HTTP阻断机制的方法,本文给大家分享问题原因及解决方案,感兴趣的朋友一... 目录一、问题场景:升级 Maven 后构建失败二、解决方案:通过 <mirror> 配置覆盖默认行为1. 配置示

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

Mybatis Plus Join使用方法示例详解

《MybatisPlusJoin使用方法示例详解》:本文主要介绍MybatisPlusJoin使用方法示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录1、pom文件2、yaml配置文件3、分页插件4、示例代码:5、测试代码6、和PageHelper结合6

Java中实现线程的创建和启动的方法

《Java中实现线程的创建和启动的方法》在Java中,实现线程的创建和启动是两个不同但紧密相关的概念,理解为什么要启动线程(调用start()方法)而非直接调用run()方法,是掌握多线程编程的关键,... 目录1. 线程的生命周期2. start() vs run() 的本质区别3. 为什么必须通过 st

如何自定义一个log适配器starter

《如何自定义一个log适配器starter》:本文主要介绍如何自定义一个log适配器starter的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求Starter 项目目录结构pom.XML 配置LogInitializer实现MDCInterceptor

C#之List集合去重复对象的实现方法

《C#之List集合去重复对象的实现方法》:本文主要介绍C#之List集合去重复对象的实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C# List集合去重复对象方法1、测试数据2、测试数据3、知识点补充总结C# List集合去重复对象方法1、测试数据

SpringBoot读取ZooKeeper(ZK)属性的方法实现

《SpringBoot读取ZooKeeper(ZK)属性的方法实现》本文主要介绍了SpringBoot读取ZooKeeper(ZK)属性的方法实现,强调使用@ConfigurationProperti... 目录1. 在配置文件中定义 ZK 属性application.propertiesapplicati