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

源码下载:

上一篇:iOS UISlider和UIStepper控件
下一篇:iOS UISwitch控件

发表评论

最新留言

很好
[***.229.124.182]2025年05月04日 11时49分43秒