iOS开发基础控件--UISegmentedControl

分段控件是我们常用的控件之一,今天把具体用法总结了下:
1.初始化UISegmentedControl
- NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",nil];
- UISegmentedControl *segmentedTemp = [[UISegmentedControl alloc]initWithItems:segmentedArray];
- self.segmentedControl = segmentedTemp;
- segmentedControl.frame = CGRectMake(10.0, 10.0, 300.0, 29.0);
- 2.常用属性及设置方法如下:
- //设置指定索引的题目
- [segmentedControl setTitle:@"1" forSegmentAtIndex:1];
- //设置指定索引的图片
- [segmentedControl setImage:[UIImage imageNamed:@"home.png"] forSegmentAtIndex:2];
- //在指定索引插入一个选项并设置图片
- [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"more.png"] atIndex:2 animated:NO];
- //在指定索引插入一个选项并设置题目
- [segmentedControl insertSegmentWithTitle:@"new" atIndex:3 animated:NO];
- //移除指定索引的选项
- [segmentedControl removeSegmentAtIndex:0 animated:NO];
- //设置指定索引选项的宽度
- [segmentedControl setWidth:60.0 forSegmentAtIndex:2];
- //设置选项中图片等的左上角的位置
- //[segmentedControl setContentOffset:CGSizeMake(10.0,10.0) forSegmentAtIndex:1];
- //设置默认选择项索引
- segmentedControl.selectedSegmentIndex = 2;
- //分段控件的颜色,只有样式为UISegmentedControlStyleBar的时候才有效果
- segmentedControl.tintColor = [UIColor redColor];
- //设置样式
- segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;
- //设置在点击后是否恢复原样
- segmentedControl.momentary = NO;
- //设置指定索引选项不可选
- [segmentedControl setEnabled:NO forSegmentAtIndex:3];
- //判断指定索引选项是否可选
- BOOL enableFlag = [segmentedControl isEnabledForSegmentAtIndex:3];
- NSLog(@"%d",enableFlag);
3.分段控件点击事件:
- [segmentedControl addTarget:self
- action:@selector(segmentAction:)
- forControlEvents:UIControlEventValueChanged];
响应的事件:
- -(void)segmentAction:(UISegmentedControl *)Seg
- {
- NSInteger index = Seg.selectedSegmentIndex;
- switch (index) {
- case 0:
- NSLog(@"0 clicked.");
- break;
- case 1:
- NSLog(@"1 clicked.");
- break;
- case 2:
- NSLog(@"2 clicked.");
- break;
- case 3:
- NSLog(@"3 clicked.");
- break;
- case 4:
- NSLog(@"4 clicked.");
- break;
- default:
- break;
- }
- }
4.获取分段控件相应的值:
- //获取指定索引选项的图片imageForSegmentAtIndex:
- UIImageView *imageForSegmentAtIndex = [[UIImageView alloc]initWithImage:[segmentedControl imageForSegmentAtIndex:1]];
- imageForSegmentAtIndex.frame = CGRectMake(60.0, 100.0, 30.0, 30.0);
- //获取指定索引选项的标题titleForSegmentAtIndex
- UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(100.0, 100.0, 30.0, 30.0)];
- titleForSegmentAtIndex.text = [segmentedControl titleForSegmentAtIndex:0];
- //获取总选项数segmentedControl.numberOfSegments
- UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(140.0, 100.0, 30.0, 30.0)];
- numberOfSegments.text = [NSString stringWithFormat:@"%d",segmentedControl.numberOfSegments];
- //获取指定索引选项的宽度widthForSegmentAtIndex:
- UILabel *widthForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(180.0, 100.0, 70.0, 30.0)];
- widthForSegmentAtIndex.text = [NSString stringWithFormat:@"%f",[segmentedControl widthForSegmentAtIndex:2]];
但是这样的分段控件只有固定的几种样式。在IOS5以后,可以全局的设置一些控件的外观,分段控件就是其中一个(全局设置UISegmentedControl外观):
- //cap insets用来指定哪些区域是固定不变的,未制定的区域则会repeat
- UIImage *segmentSelected = [[UIImage imageNamed:@"bg_o.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
- UIImage *segmentUnselected = [[UIImage imageNamed:@"bg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
- UIImage *segmentSelectedUnselected = [UIImage imageNamed:@"line.png"] ;
- UIImage *segUnselectedSelected = [UIImage imageNamed:@"line.png"] ;
- UIImage *segmentUnselectedUnselected = [UIImage imageNamed:@"line.png"];
- //Segmente未选中背景
- [[UISegmentedControl appearance] setBackgroundImage:segmentUnselected
- forState:UIControlStateNormal
- barMetrics:UIBarMetricsDefault];
- //Segmente选中背景
- [[UISegmentedControl appearance] setBackgroundImage:segmentSelected
- forState:UIControlStateSelected
- barMetrics:UIBarMetricsDefault];
- //Segmente左右都未选中时的分割线
- //BarMetrics表示navigation bar的状态,UIBarMetricsDefault 表示portrait状态(44pixel height),UIBarMetricsLandscapePhone 表示landscape状态(32pixel height)
- [[UISegmentedControl appearance] setDividerImage:segmentUnselectedUnselected
- forLeftSegmentState:UIControlStateNormal
- rightSegmentState:UIControlStateNormal
- barMetrics:UIBarMetricsDefault];
- [[UISegmentedControl appearance] setDividerImage:segmentSelectedUnselected
- forLeftSegmentState:UIControlStateSelected
- rightSegmentState:UIControlStateNormal
- barMetrics:UIBarMetricsDefault];
- [[UISegmentedControl appearance] setDividerImage:segUnselectedSelected
- forLeftSegmentState:UIControlStateNormal
- rightSegmentState:UIControlStateSelected
- barMetrics:UIBarMetricsDefault];
- //字体
- NSDictionary *textAttibutesUnSelected = [NSDictionary dictionaryWithObjectsAndKeys:
- [UIFont systemFontOfSize:18],UITextAttributeFont,
- [UIColor blackColor],UITextAttributeTextColor,
- [UIColor whiteColor],UITextAttributeTextShadowColor,
- [NSValue valueWithCGSize:CGSizeMake(1, 1)],UITextAttributeTextShadowOffset,nil];
- NSDictionary *textAttibutesSelected = [NSDictionary dictionaryWithObjectsAndKeys:
- [UIFont systemFontOfSize:18],UITextAttributeFont,
- [UIColor whiteColor],UITextAttributeTextColor,
- [UIColor whiteColor],UITextAttributeTextShadowColor,
- [NSValue valueWithCGSize:CGSizeMake(0, 0)],UITextAttributeTextShadowOffset,nil];
- [[UISegmentedControl appearance] setTitleTextAttributes:textAttibutesUnSelected
- forState:UIControlStateNormal];
- [[UISegmentedControl appearance] setTitleTextAttributes:textAttibutesSelected
- forState:UIControlStateSelected];
iOS开发基础控件--UISegmentedControl的更多相关文章
- iOS开发基础控件--UIButton
01 //这里创建一个圆角矩形的按钮 02 UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 03 ...
- iOS开发基础控件--UILabel
UILabel 的常见属性和方法: //创建UIlabel对象 UILabel* label = [[UILabel alloc] initWithFrame:self.view.bounds]; / ...
- iOS开发基础控件--UITextField
001 //初始化textfield并设置位置及大小 002 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20 ...
- iOS:分段控件UISegmentedControl的详细使用
分段控件:UISegmentedControl 功能:分段的控制.页面的切换等. 介绍:当用户输入不仅仅是布尔值时,可使用分段控件(UISegmentedControl).分段控件提供一栏按钮 ...
- IOS(一) 基础控件的介绍以及使用
IOS的界面的制作,相对于Android来说 简洁了很多,虽然创建布局的方式都是两种(代码创建.布局文件) 但是Android中的xml布局文件在某些方面也属于代码创建,因为自己使用到得每一个属性 都 ...
- 【ios开发】控件细究1:UITableView
工作了将近两个月,共接手两个项目,在项目中用的最多的就是UITableView了,但是也是问题出现的最多的地方,由于一开始不熟练,导致很多问题花了很长时间才解决.所以利用这两天空闲时间,好好梳理一下这 ...
- iOS开发-DatePicker控件
时间控件不管是Android还是iOS中都是必然存在的一个控件,具体的效果大同小异,显示日期,时间,iOS中有四种方式可以选择,Time, Date,Date and Time , Count Do ...
- IOS开发之控件篇UINavigationController第一章 - 介绍
UINavigationController是一个比较常见的控件,它连接个视图,例如一个视图走到另外一个视图,之间的联系都可以用这个NavigationController的方法 一般都会由两个部分组 ...
- IOS开发之控件篇UICollectionViewControllor第一章 - 普通介绍
1.介绍 UICollectionView和UICollectionViewControllor是IOS6.0后引入的新控件 使用UICollectionView必须实现三个接口: UICollect ...
随机推荐
- Django 打印
转自:http://bbs.chinaunix.net/archiver/tid-1227401.html fentin 发表于 2008-07-28 17:52:44 请教Django Python ...
- Open Asset Import Library(assimp) vs2010编译
Assimp(Open Asset Import Library)是一个开源的3D模型导入解析库, 可以处理很多种3D文件格式:Collada, Blend, Obj, X, 3DS, LWO, MD ...
- Numpy, Pandas, Matplotlib, Scipy 初步
Numpy: 计算基础, 以类似于matlab的矩阵计算为基础. 底层以C实现, 速度快. Pandas: 以numpy为基础, 扩充了很多统计工具. 重点是数据统计分析. Matplotlib: ...
- c++ queue 用法
最重要的是: queue 和 stack 都没有迭代器!!! 1. push 队列中由于是先进先出,push即在队尾插入一个元素. 2. pop 将队列中最靠前位置的元素拿掉,和push都是没有返回值 ...
- nginx grpc 试用
1. 编译 wget https://nginx.org/download/nginx-1.13.10.tar.gz tar xvf nginx-1.13.10.tar.gz cd nginx-1.1 ...
- (转)Linux安装SwfTools-0.9.2安装事,在执行make install时报错
系统:CentOS6.5 安装SwfTools-0.9.2的时候,在执行make install时报错, rm -f /usr/local/share/swftools/swfs/default_vi ...
- erlang里面中文相关处理
在控制台输出的话 Name = "测试数据", io:format("~ts~n",[Name]). 如果是和客户端通信,假如都是utf8编码 服务器获取的时候 ...
- 工作JS总结
获取 <inpout type="checkbox" value="1" /> 多选项的value /*获取checkbox的全部选中项 使用方法: ...
- 为什么KVM计算机点无故重启?
一.故障1:机器hangs 本地一台cloudstack计算节点无故连不上了,cloudstack也坏了,后查看有一台系统虚拟机在这台计算节点上,导致cs挂了.去找到这台机器后,发现这台机器卡住了,重 ...
- python学习笔记(八):异常处理
一.异常处理 在程序运行过程中,总会遇到各种各样的错误.程序一出错就停止运行了,那我们不能让程序停止运行吧,这时候就需要捕捉异常了,通过捕捉到的异常,我们再去做对应的处理. 下面我们先写一个函数,实现 ...
