iOS UISegmentControl如何实现自定义分栏效果
发表于:2025-11-21 作者:千家信息网编辑
千家信息网最后更新 2025年11月21日,小编给大家分享一下iOS UISegmentControl如何实现自定义分栏效果,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下
千家信息网最后更新 2025年11月21日iOS UISegmentControl如何实现自定义分栏效果
小编给大家分享一下iOS UISegmentControl如何实现自定义分栏效果,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
具体内容如下

iOS 自带的UISegmentControl实现的就是类似上图的效果
但是很多用处 一般这两个分栏是两个tableView,需要左右滑动来响应分栏
下面来讲述这样的效果是怎么实现的呢?

主要那里有一根短红线,需要滑动 来切换tableView
先自定义一个SegmentView
.h
//定义block,用来传递点击的第几个按钮typedef void (^PassValueBlock)(NSInteger index);@interface BCLCommunitySegmentView : UIView//定义一下block@property (nonatomic, strong) PassValueBlock returnBlock;@property (nonatomic, strong) UIImageView *selectImage;//这个就是短红线//初始化数组,传入frame和名称- (id) initWithFrame:(CGRect)frame withTitleArray:(NSArray *)array;//block传递值方法- (void)setReturnBlock:(PassValueBlock)returnBlock;@end
在SegmentView.m中
循环创建按钮,几个分栏创建几个按钮
- (void)creatSegmentView { //设置按钮的宽度 _itemWidth = self.frame.size.width / _itemCounts; //循环创建按钮 for (int i = 0; i < _itemCounts; i++) { UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake((i + 1) *_itemWidth/2, 0, _itemWidth/2, self.frame.size.height)]; [self addSubview:button]; //设置button的字 [button setTitle:_titleArray[i] forState:UIControlStateNormal]; //设置button的字颜色 [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; //设置字体大小 button.titleLabel.font = [UIFont systemFontOfSize:20]; //设置居中显示 button.titleLabel.textAlignment = NSTextAlignmentCenter; //设置tag值 button.tag = 1000 + i; //添加点击事件 [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; //如果是第一个,默认被选中 if (i == 0) { button.selected = YES; } } //添加一个select _selectImage = [[UIImageView alloc]initWithFrame:CGRectMake(_itemWidth / 2, self.frame.size.height - 2, _itemWidth / 2, 2)]; _selectImage.image = [UIImage imageNamed:@"bcl_bg_community_segment_color_line"]; [self addSubview:_selectImage];}然后设置按钮的点击事件,将点击到哪个按钮 回调过去
-(void)buttonAction:(UIButton *)button{ //当button被点击,所有的button都设为未选中状态 for (UIView *view in self.subviews) { if ([view isKindOfClass:[UIButton class]]) { UIButton *subButton = (UIButton*)view; subButton.selected = NO; subButton.titleLabel.font = [UIFont systemFontOfSize:20]; } } //然后将选中的这个button变为选中状态 button.selected = YES; //通过当前的tag值设置select的位置 NSInteger index = button.tag - 1000; [UIView animateWithDuration:0.3 animations:^{ self->_selectImage.frame = CGRectMake((1 + index)*_itemWidth/2, _selectImage.frame.origin.y, self->_selectImage.frame.size.width, _selectImage.frame.size.height); }]; _returnBlock(index);}在需要展现的controller中
.h
@interface BCLCommunityView : UIView@property (nonatomic, strong) UIScrollView *scrollerView;@property(nonatomic ,strong) UITableView *circleTableView;@property(nonatomic ,strong) UITableView *squreTableView;@property (nonatomic, strong)BCLCommunitySegmentView *segmentView;@end
在.m中用scrollView实现分栏的两个tableView的滑动
- (instancetype) initWithFrame:(CGRect)frame { if(self = [super initWithFrame:frame]) { [self setSegmentView]; _circleTableView = [self loadTableView]; _squreTableView = [self loadTableView]; _circleTableView.tag = 1; _squreTableView.tag = 2; _scrollerView = [[UIScrollView alloc] init]; _scrollerView.frame = CGRectMake(0, 104, KScreenW, KScreenH); _scrollerView.pagingEnabled = YES; _scrollerView.scrollEnabled = YES; _scrollerView.contentSize = CGSizeMake(KScreenW * 2, KScreenH); _scrollerView.bounces = YES; _scrollerView.delegate = self; [_scrollerView addSubview:_circleTableView]; [_scrollerView addSubview:_squreTableView]; _circleTableView.frame = CGRectMake(0, 0, KScreenW, KScreenH); _squreTableView.frame = CGRectMake(KScreenW, 0, KScreenW, KScreenH); [self addSubview:_scrollerView]; } return self;}- (UITableView *)loadTableView{ UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, KScreenH) style:UITableViewStyleGrouped]; tableView.showsVerticalScrollIndicator = NO; [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; tableView.dataSource = self; [self addSubview:tableView]; return tableView;}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if(tableView.tag == 1) { return 3; } else { return 2; } }- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if(tableView.tag == 1) { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; if(!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; } cell.backgroundColor = [UIColor redColor]; cell.textLabel.text = @"11111"; return cell; } else { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; if(!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; } return cell; } }scrollView代理 滑动scrollerView实现小红条的滑动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { CGRect frame = _segmentView.selectImage.frame; if(scrollView.contentOffset.x / KScreenW == 0) { [UIView animateWithDuration:0.1 animations:^{ _segmentView.selectImage.frame = CGRectMake(KScreenW / 4, frame.origin.y, frame.size.width, frame.size.height); }]; } else if(scrollView.contentOffset.x / KScreenW == 1){ [UIView animateWithDuration:0.1 animations:^{ _segmentView.selectImage.frame = CGRectMake(KScreenW / 2, frame.origin.y, frame.size.width, frame.size.height); }]; }}以上是"iOS UISegmentControl如何实现自定义分栏效果"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
按钮
效果
两个
内容
篇文章
事件
就是
状态
红线
循环
上图
不怎么
中用
位置
名称
大小
大部分
字体
宽度
数组
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
苏州中国移动软件开发中心
网络安全实训报告实训总结
帝国神话自建服务器卡在加载
比顺丰科技好的互联网
奉贤区微型软件开发生产厂家
软件开发运维费用
spark连接华为云数据库
群晖NAS 安装 数据库
网络安全文明 海报
蜀山区网络技术咨询热线
锁屏背单词软件开发
服务器必须备案吗
数据库成绩查询系统实习
如何查看druid连接的数据库
nacos怎么配置数据库
我的世界服务器npc名字
数据库的综训目的
2008配置远程服务器
西安源鼎互联网科技有限公司
服务器管理系统怎么打开
数据库怎么改字段的一个值
2019网络安全受益股
网络安全例行检查制度
java连接数据库登录
塔科夫怎样更换服务器
数据库慕课单元作业中南大学
幼儿园网络安全管理规范
数据库的综训目的
榆林管理软件开发
浪潮塔式服务器有哪些