iOS UISegmentControl如何实现自定义分栏效果
发表于:2026-11-05 作者:千家信息网编辑
千家信息网最后更新 2026年11月05日,小编给大家分享一下iOS UISegmentControl如何实现自定义分栏效果,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下
千家信息网最后更新 2026年11月05日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安全错误
数据库的锁怎样保障安全
互联网科技公司起名
互联网为科技
朗新科技与工业互联网
互联网科技战争
碳银互联网科技
互联网科技大会
科技类的互联网平台
互联网之后的科技是什么
湖南诚度互联网科技有限公司
成都铂石互联网科技做什么的
上海宁丞科技互联网
河南猎农互联网科技有限公司
深圳市罗太互联网科技有限公司
皮皮虾互联网科技有限公司
互联网科技资金流入
本届互联网大会上的黑科技
云鲸互联网络科技有限公司
科技互联网行业投资研究
第三届互联网大会安防黑科技
科技互联网专业务虚会材料
互联网加科技
互联网科技保险
融势互联网科技
博兴的互联网电子科技公司
江苏南京365互联网科技
健康食品互联网创新科技园项目
杭州披云加互联网科技有限公司
版纳互联网科技选哪家
互联网科技职业技能培训学校
科技和互联网行业的特点