UISegmentedControl的具体使用
当用户输入不不过布尔值时。可使用分段控件(UISegmentedControl)。分段控件提供一栏button(有时称为button栏),但只能激活当中一个button。
分段控件会导致用户在屏幕上看到的内容发生变化。
它们经常使用于在不同类别的信息之间选择,或在不同的应用屏幕之间切换。
以下介绍基本属性和基本方法的使用。
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;//设置样式

//segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;//设置样式

//segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;//设置样式

//segmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;//设置样式

// segmentedControl.momentary = YES;//设置在点击后是否恢复原样
[segmentedControl setTitle:@"two" forSegmentAtIndex:1];//设置指定索引的题目

[segmentedControl setImage:[UIImage imageNamed:@"btn_jyy.png"] forSegmentAtIndex:3];//设置指定索引的图片

[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"mei.png"] atIndex:2 animated:NO];//在指定索引插入一个选项并设置图片

[segmentedControl insertSegmentWithTitle:@"insert" atIndex:3 animated:NO];//在指定索引插入一个选项并设置题目

[segmentedControl removeSegmentAtIndex:0 animated:NO];//移除指定索引的选项

// [segmentedControl setWidth:70.0 forSegmentAtIndex:2];//设置指定索引选项的宽度
// [segmentedControl setContentOffset:CGSizeMake(10.0,10.0) forSegmentAtIndex:4];//设置选项中图片等的左上角的位置
//获取指定索引选项的图片imageForSegmentAtIndex:
UIImageView *imageForSegmentAtIndex = [[UIImageViewalloc]initWithImage:[segmentedControl imageForSegmentAtIndex: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",[segmentedControl widthForSegmentAtIndex: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
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];
UISegmentedControl的具体使用的更多相关文章
- iOS在导航栏上居中显示分段控件(UISegmentedControl)
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:nil]; segmentedCont ...
- 【UISegmentedControl】- 分段控件
一.初始化 二.常见的属性 1.segmentedControlStyle属性:设置基本的样式 2.momentary属性:设置在点击后是否恢复原样 . 3.numberOfSegments属性:只读 ...
- UI控件(UISegmentedControl)
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSArray* segmentArray = [[ ...
- UI第八节——UISegmentedControl
- (void)viewDidLoad { [super viewDidLoad]; NSArray *items = @[@"消息", @"电话" ...
- UISegmentedControl 的使用
/** 设置选择器 */ - (void)setUpSegmentCtr { UISegmentedControl *segmentCtr = [[UISegmentedControl alloc] ...
- UISegmentedControl和UIStepper的使用
UISegmentedControl:分栏控件,常用的属性和方法是 1.tintColor:控制分栏控件的颜色风格 2.insertSegmentWithTitle(Image):插入分栏标题(图片) ...
- UISegmentedControl 控件
一.创建 UISegmentedControl* mySegmentedControl = [[UISegmentedControl alloc]initWithItems:nil]; 是不是很奇怪没 ...
- UI中一些不常用的控件UIActivityIndicatorView、UIProgressView、UISegmentedControl、UIStepper、UISwitch、UITextView、UIAlertController
//UIActivityIndicatorView //小菊花,加载 #import "ActivityIndicatorVC.h" @interface ActivityIndi ...
- UISegmentedControl(人物简介)
效果图 当你点击上面人物名字的时候 ,就可以随意切换人物. 这个很有趣 , 你还可以试着添加音乐播放器 .以及一些别的来完善你想做的. 好吧 , 废话不多说 , 上代码. #import " ...
- UILabel UISwitch UISegmentedControl UIAlertView
基础小控件 /***************************************UIlabel*************************************/ UILabel ...
随机推荐
- pat 甲级 1009. Product of Polynomials (25)
1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- pat 团体天梯赛 L2-011. 玩转二叉树
L2-011. 玩转二叉树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜 ...
- nginx和php安装文件
#!/usr/bin/env bash echo "=============START=====================" ## php echo '[php]yum i ...
- 【转】利用ScriptManager实现Javascript调用WebService中的方法
原文发布时间为:2009-07-01 -- 来源于本人的百度文章 [由搬家工具导入] 前台调用后台方法,或者后台调用前台方法。ScriptManager实现 开发过程中,总想在前台直接调用后台的met ...
- 利用C#原有函数对数组进行降序排列
原文发布时间为:2009-03-04 -- 来源于本人的百度文章 [由搬家工具导入] 利用 Array类的静态方法 Sort可以对数组进行升序排列。如下: Array.Sort(数组名); ...
- C语言字符串操作总结大全(超详细)【转】
转自:http://www.jb51.net/article/37410.htm )字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strc ...
- linux2.4内核调度
进程调度需要兼顾3种进程:交互进程,批处理进程,实时进程,在设计一个进程调度机制时需要考虑具体问题 (1)调度时机? 答:进程在用户空间可以pause()或者让内核设置进程为睡眠状态,以此调度,调度还 ...
- Android Studio查看其它APP的布局结构
概述 日常使用别家的APP过程中,会遇到一些比较好看的布局,这时候我们就想学习一下别人的布局结构,以便参考. (1)手机连接电脑.设置手机为USB调试模式 (2)运行Android Studio,打开 ...
- C# ASP.NET中Process.Start没有反应也没有报错的解决方法
最近有一个很坑的需求,在ASP.NET中打开一个access,还要用process.start打开,调试时一切正常,到了发布后就没有反应,找了一下午,各种设文件夹权限也不行,最后把应用程序池改成管理员 ...
- linux挂载新磁盘、分区和开机自动挂载
今天在阿里云虚拟主机里新加了一块磁盘,需要单独挂载到centos7. 挂载过程中遇到了不少问题,记录如下: 查看分区 fdisk -l 其中第一个框和第二个框,是已经分好区的磁盘,第三个硬盘没有分区. ...