
iOS UIPageControl和UISegmentedControl控件
添加删除选项
发布日期:2021-05-14 01:35:54
浏览次数:19
分类:精选文章
本文共 6561 字,大约阅读时间需要 21 分钟。
1. UIPageControl控件
UIPageControl
控件提供一行点来提示当前显示的多个页面中的哪一页。
1.1 主要属性
属性 | 说明 |
---|---|
numberOfPages | 小白点的数量 |
currentPage | 当前选中的点 |
pageIndicatorTintColor | 未选中小白点的颜色 |
currentPageIndicatorTintColor | 当前选中小白点的颜色 |
hidesForSinglePage | 只有一个页面时隐藏控件(默认是NO) |
设置点的数量和颜色,
UIPageControl* pageControl = [[UIPageControl alloc] initWithFrame:rect];pageControl.pageIndicatorTintColor = [UIColor grayColor];pageControl.currentPageIndicatorTintColor = [UIColor magentaColor];pageControl.numberOfPages = 6;pageControl.currentPage = 2;
显示如下

1.2 iOS14新特性
背景样式UIPageControlBackgroundStyle
typedef NS_ENUM(NSInteger, UIPageControlBackgroundStyle) { /// The default background style that adapts based on the current interaction state. UIPageControlBackgroundStyleAutomatic = 0, /// The background style that shows a full background regardless of the interaction UIPageControlBackgroundStyleProminent = 1, /// The background style that shows a minimal background regardless of the interaction UIPageControlBackgroundStyleMinimal = 2,}
UIPageControlBackgroundStyleProminent
为控件添加背景,显示如下

交互方式UIPageControlInteractionState
typedef NS_ENUM(NSInteger, UIPageControlInteractionState) { /// The default interaction state, where no interaction has occured. UIPageControlInteractionStateNone = 0, /// The interaction state for which the page was changed via a single, discrete interaction. UIPageControlInteractionStateDiscrete = 1, /// The interaction state for which the page was changed via a continuous interaction. UIPageControlInteractionStateContinuous = 2,}
交互方式有两种离散和连续,通过设置属性allowsContinuousInteraction
修改,默认是YES
离散(UIPageControlInteractionStateDiscrete
),每次只能移动一格

连续(UIPageControlInteractionStateContinuous
),可以通过拖拽快速移动

自定义样式
UIPageControl
还可以自定义小圆点,使用图片替代
属性preferredIndicatorImage
可以自定义图片

或者通过setIndicatorImage: forPage:
方法对不同位置的自定义图片

1.3 监听点击事件
[PageControl addTarget:self action:@selector(onPageControlValueChange:) forControlEvents:UIControlEventValueChanged];
onPageControlValueChange:
方法监听
-(void)onPageControlValueChange: (UIPageControl*) sender { NSLog(@"currentPage = %ld", sender.currentPage);}
2. UISegmentedControl控件
UISegmentedControl
控件用于界面之间的逻辑切换。
2.1 主要属性
属性 | 说明 |
---|---|
selectedSegmentIndex | 默认选择项 |
selectedSegmentTintColor | 选中项背景色 |
momentray | 选中后是否自动释放,默认NO |
apportionsSegmentWidthsByContent | 是否根据内容定分段宽度,默认是NO |
tintColor | 控件渲染色,ios13后失效 |
numberOfSegments | 选项数量,只读 |
示例代码
NSArray* titleArray = @[@"东", @"南", @"西", @"北", @"这个选项有点长"];UISegmentedControl* normalSegmentedControl = [[UISegmentedControl alloc] initWithItems:titleArray];normalSegmentedControl.frame = CGRectMake(10, 100, [UIScreen mainScreen].bounds.size.width-20 , 44);normalSegmentedControl.selectedSegmentIndex = 1;[self.view addSubview:normalSegmentedControl];UISegmentedControl* selectSegmentedControl = [[UISegmentedControl alloc] initWithItems:titleArray];selectSegmentedControl.frame = CGRectMake(10, 180, [UIScreen mainScreen].bounds.size.width-20 , 44);selectSegmentedControl.selectedSegmentIndex = 1;selectSegmentedControl.selectedSegmentTintColor = [UIColor magentaColor];[self.view addSubview:selectSegmentedControl];UISegmentedControl* momentarySegmentedControl = [[UISegmentedControl alloc] initWithItems:titleArray];momentarySegmentedControl.frame = CGRectMake(10, 260, [UIScreen mainScreen].bounds.size.width-20 , 44);momentarySegmentedControl.momentary = YES;[self.view addSubview:momentarySegmentedControl];UISegmentedControl* apportionsSegmentedControl = [[UISegmentedControl alloc] initWithItems:titleArray];apportionsSegmentedControl.frame = CGRectMake(10, 340, [UIScreen mainScreen].bounds.size.width-20 , 44);apportionsSegmentedControl.selectedSegmentIndex = 1;apportionsSegmentedControl.apportionsSegmentWidthsByContent = YES;[self.view addSubview:apportionsSegmentedControl];
显示如下

2.2 主要方法
方法 | 说明 |
---|---|
setTitle:(NSString *) forSegmentAtIndex:(NSUInteger) | 在指定索引设置标题 |
setImage:(UIImage *) forSegmentAtIndex:(NSUInteger) | 在指定索引设置图片 |
setWidth:(CGFloat) forSegmentAtIndex:(NSUInteger) | 在指定索引设置宽度 |
setContentOffset:(CGSize) forSegmentAtIndex:(NSUInteger) | 在指定索引设置偏移 |
setBackgroundImage:(UIImage *) forState:(UIControlState) barMetrics:(UIBarMetrics) | 设置背景色 |
setDividerImage:(UIImage *) forLeftSegmentState:(UIControlState) rightSegmentState:(UIControlState) barMetrics:(UIBarMetrics) | 设置分割线 |
setTitleTextAttributes:(NSDictionary<NSAttributedStringKey,id> *) forState:(UIControlState) | 设置标题的文本属性 |
示例代码
NSArray* titleArray = @[@"东", @"南", @"西", @"北", @"这个选项有点长"];UISegmentedControl* normalSegmentedControl = [[UISegmentedControl alloc] initWithItems:titleArray];normalSegmentedControl.frame = CGRectMake(10, 100, [UIScreen mainScreen].bounds.size.width-20 , 44);[self.view addSubview:normalSegmentedControl];[normalSegmentedControl setTitle:@"选项1" forSegmentAtIndex:0];[normalSegmentedControl setImage:[UIImage imageNamed:@"icon_money"] forSegmentAtIndex:1];[normalSegmentedControl setContentOffset:CGSizeMake(10, 10) forSegmentAtIndex:2];[normalSegmentedControl setWidth:120 forSegmentAtIndex:4];[normalSegmentedControl setBackgroundImage:[[UIColor whiteColor] colorToImage] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];[normalSegmentedControl setBackgroundImage:[[UIColor blueColor] colorToImage] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];[normalSegmentedControl setDividerImage:[[UIColor blueColor] colorToImage] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];[normalSegmentedControl setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blueColor]} forState:UIControlStateNormal];[normalSegmentedControl setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor]} forState:UIControlStateSelected];normalSegmentedControl.layer.borderWidth = 1;normalSegmentedControl.layer.borderColor = [UIColor blueColor].CGColor;normalSegmentedControl.selectedSegmentIndex = 0;
显示如下

方法 | 说明 |
---|---|
insertSegmentWithTitle:(NSString *) atIndex:(NSUInteger) animated:(BOOL) | 在指定索引插入一个选项并设置题目 |
insertSegmentWithImage:(UIImage *) atIndex:(NSUInteger) animated:(BOOL) | 在指定索引插入一个选项并设置图片 |
removeSegmentAtIndex:(NSUInteger) animated:(BOOL) | 删除指定索引选项 |
removeAllSegments | 删除所有选项 |
2.3 监听点击事件
[UISegmentedControl addTarget:self action:@selector(onSegmentedControlValueChange:) forControlEvents:UIControlEventValueChanged];}
onSegmentedControlValueChange:
方法监听
-(void)onSegmentedControlValueChange: (UISegmentedControl*) sender { NSLog(@"selectedSegmentIndex = %ld", sender.selectedSegmentIndex);}
源码下载:
发表评论
最新留言
很好
[***.229.124.182]2025年05月04日 11时49分43秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
pycharm使用(新建工程、字体修改、调试)
2021-05-14
什么是Numpy、Numpy教程
2021-05-14
Python学习笔记——元组
2021-05-14
异常声音检测
2021-05-14
PCB学习笔记——AD17如何添加新的封装
2021-05-14
numpy版本问题
2021-05-14
无法打开文件“opencv_world330d.lib”的解决办法
2021-05-14
maven项目通过Eclipse上传到svn上面,再导入到本地出现指定的类找不到的问题
2021-05-14
maven 项目部署到tomcat下 没有class文件
2021-05-14
算法训练 未名湖边的烦恼(递归,递推)
2021-05-14
算法训练 完数(循环,数学知识)
2021-05-14
什么是接口
2021-05-14
2020版nodejs12.18.3安装配置教程
2021-05-14
iview组件库中,Form组件里的Input,无法正确绑定on-enter事件
2021-05-14
记录-基于springboot+vue.js实现的超大文件分片极速上传及流式下载
2021-05-14
JavaScript高级程序设计第四版学习记录-第九章代理与反射
2021-05-14
怎么解决Windows 10文件/文件夹正在使用无法删除
2021-05-14
F28335第九篇——通用IO
2021-05-14