转自:http://www.cnblogs.com/aipingguodeli/archive/2012/04/12/2443687.html
当用户输入不仅仅是布尔值时,可使用分段控件 (UISegmentedControl)。分段控件提供一栏按钮(有时称为按钮栏),但只能激活其中一个按钮。分段控件会导致用户在屏幕上看到的内容发 生变化。它们常用于在不同类别的信息之间选择,或在不同的应用屏幕之间切换。下面介绍基本属性和基本方法的使用。
NSArray *segmentedArray = [[NSArrayalloc]initWithObjects:@"1",@"2",@"3",@"4",nil];
//初始化UISegmentedControl
UISegmentedControl *segmentedControl = [[UISegmentedControlalloc]initWithItems:segmentedArray];
segmentedControl.frame = CGRectMake(20.0, 20.0, 250.0, 50.0);
segmentedControl.selectedSegmentIndex = 2;//设置默认选择项索引
segmentedControl.tintColor = [UIColor redColor];
//有基本四种样式
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;//设置样式
![UISegmentedControl的详细使用](https://i-blog.csdnimg.cn/blog_migrate/ef2ae98e66c3c1eb8885035d63475838.png)
//segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;//设置样式
![UISegmentedControl的详细使用](https://i-blog.csdnimg.cn/blog_migrate/068d9144ba1b495e526a31b0a6303f97.png)
//segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;//设置样式
![UISegmentedControl的详细使用](https://i-blog.csdnimg.cn/blog_migrate/7b52b6ce4fd49e356bf870ce9309a919.png)
//segmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;//设置样式
![UISegmentedControl的详细使用](https://i-blog.csdnimg.cn/blog_migrate/e55cd1898122104f3f2fde28b38a2110.png)
// segmentedControl.momentary = YES;//设置在点击后是否恢复原样
[segmentedControl setTitle:@"two" forSegmentAtIndex:1];//设置指定索引的题目
![UISegmentedControl的详细使用](https://i-blog.csdnimg.cn/blog_migrate/6d6316d7ca1000ab4ac418a6d9bf8f4d.png)
[segmentedControl setImage:[UIImage imageNamed:@"btn_jyy.png"] forSegmentAtIndex:3];//设置指定索引的图片
![UISegmentedControl的详细使用](https://i-blog.csdnimg.cn/blog_migrate/ce719f50222e9d0270d75ed65568d843.png)
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"mei.png"] atIndex:2 animated:NO];//在指定索引插入一个选项并设置图片
![UISegmentedControl的详细使用](https://i-blog.csdnimg.cn/blog_migrate/cbb6eb965f5918aa7e3bf49768ad0ff9.png)
[segmentedControl insertSegmentWithTitle:@"insert" atIndex:3 animated:NO];//在指定索引插入一个选项并设置题目
![UISegmentedControl的详细使用](https://i-blog.csdnimg.cn/blog_migrate/98fe336f1e95d513ef1c31f3a6b938f9.png)
[segmentedControl removeSegmentAtIndex:0 animated:NO];//移除指定索引的选项
![UISegmentedControl的详细使用](https://i-blog.csdnimg.cn/blog_migrate/81259621dad3c8c876781d34177e180a.png)
// [segmentedControl setWidth:70.0 forSegmentAtIndex:2];//设置指定索引选项的宽度
// [segmentedControl setContentOffset:CGSizeMake(10.0,10.0) forSegmentAtIndex:4];//设置选项中图片等的左上角的位置
//获取指定索引选项的图片imageForSegmentAtIndex:
UIImageView *imageForSegmentAtIndex = [[UIImageViewalloc]initWithImage:[segmentedControlimageForSegmentAtIndex:1]];
imageForSegmentAtIndex.frame = CGRectMake(60.0, 120.0, 30.0, 30.0); ;
//获取指定索引选项的标题titleForSegmentAtIndex
UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(100.0, 160.0, 30.0, 30.0)];
titleForSegmentAtIndex.text = [segmentedControl titleForSegmentAtIndex:0];
//获取总选项数segmentedControl.numberOfSegments
UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(140.0, 170.0, 30.0, 30.0)];
numberOfSegments.text = [NSString stringWithFormat:@"%d",segmentedControl.numberOfSegments];
//获取指定索引选项的宽度widthForSegmentAtIndex:
UILabel *widthForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(180.0, 210.0, 70.0, 30.0)];
widthForSegmentAtIndex.text = [NSString stringWithFormat:@"%f",[segmentedControlwidthForSegmentAtIndex:2]];
// [segmentedControl setEnabled:NO forSegmentAtIndex:4];//设置指定索引选项不可选
// BOOL enableFlag = [segmentedControl isEnabledForSegmentAtIndex:4];//判断指定索引选项是否可选
[mySegmentedControladdTarget:selfaction:@selector(segmentAction:)forControlEvents:UIControlEventValueChanged]; //添加委托方法
//具体委托方法实例
-(void)segmentAction:(UISegmentedControl *)Seg{
NSInteger Index = Seg.selectedSegmentIndex;
NSLog(@"Index %i", Index);
switch (Index) {
case 0:
[self selectmyView1];
break;
case 1:
[self selectmyView2];
break;
case 2:
[self selectmyView3];
break;
case 3:
[self selectmyView4];
break;
case 4:
[self selectmyView5];
break;
case 5:
[self selectmyView6];
break;
default:
break;
}
}
基本属性、方法如上,大家在实际开发中可根据实际需求选取适当属性和方法。
使用技巧:
在导航栏中添加UISegmentedControl ,实现的效果:
![UISegmentedControl的详细使用](https://i-blog.csdnimg.cn/blog_migrate/cba4d0f566bea1d1e5f4c636810dbeef.png)
实现的代码:
//自定义UISegmentedcontrol
UISegmentedControl *segmentedControl=[[UISegmentedControl alloc] initWithFrame:CGRectMake(80.0f, 8.0f, 200.0f, 30.0f) ];
[segmentedControl insertSegmentWithTitle:@"Food to eat" atIndex:0 animated:YES];
[segmentedControl insertSegmentWithTitle:@"Food to avoid" atIndex:1 animated:YES];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
segmentedControl.multipleTouchEnabled=NO;
[segmentedControl addTarget:self action:@selector(Selectbutton:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *segButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl]; //自定义UIBarButtonItem,封装定义好的UIsegmented。
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segButton; //添加到导航栏中
[segButton release];