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 ...
随机推荐
- HDU3232 Crossing Rivers 数学期望问题
Crossing Rivers ...
- MySQL Innodb表导致死锁日志情况分析与归纳
发现当备份表格的sql语句与删除该表部分数据的sql语句同时运行时,mysql会检测出死锁,并打印出日志 案例描述在定时脚本运行过程中,发现当备份表格的sql语句与删除该表部分数据的sql语句同时 ...
- no for & 100 Array & Uint8Array & Typed Arrays
no for & 100 Array http://hiluluke.cn/ bad function generate100Array() { var arr = new Array(100 ...
- PHP算法面试题目
1.使用PHP描述冒泡排序和快速排序算法,对象可以是一个数组 //冒泡排序(数组排序) functionbubble_sort($array){ $count = count($array ...
- CDOJ 1256 二维前缀和处理
昊昊喜欢运动 他NN 天内会参加MM 种运动(每种运动用一个[1,m][1,m] 的整数表示) 舍友有QQ 个问题 问昊昊第ll 天到第rr 天参加了多少种不同的运动 Input 输入两个数NN , ...
- Gym 100989F 水&愚&vector
standard input/output You must have heard about Agent Mahone! Dr. Ibrahim hired him to catch the che ...
- python百度贴吧爬虫
# -*- coding: utf-8 -*- #coding=utf-8 import urllib import urllib2 import re import thread import ti ...
- C++ vector 的 begin()、end()、front()、back() 区别
STL中实现源码可见:http://www.cplusplus.com/reference/vector/vector/begin/ 一.begin函数 函数原型: iterator begin(); ...
- Python之数据结构:序列
一.序列 1.列表.元组和字符串都是序列 二.序列的两个特点:索引操作符和切片操作符 1.索引操作符:从序列中抓取一个特定项目 下标操作:使用索引获取序列中的单个项目: eg:shoplist[0] ...
- VMWare VMNet 8 的配置使用
网上有很多讲解VMWare网络原理的文章,我在这里就不在赘述,有兴趣的朋友可以自己搜 本章主要介绍下我们使用VM最常用的两种网络模式,VMNet 0 和 VMNet 8 本文均为原创,如需转载请标明, ...