1.
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);
 
[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;
}
}
 
//获取指定索引选项的图片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]];
 
//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];

UISegmentedControl的更多相关文章

  1. iOS在导航栏上居中显示分段控件(UISegmentedControl)

    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:nil]; segmentedCont ...

  2. 【UISegmentedControl】-  分段控件

    一.初始化 二.常见的属性 1.segmentedControlStyle属性:设置基本的样式 2.momentary属性:设置在点击后是否恢复原样 . 3.numberOfSegments属性:只读 ...

  3. UI控件(UISegmentedControl)

    @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSArray* segmentArray = [[ ...

  4. UI第八节——UISegmentedControl

    - (void)viewDidLoad {    [super viewDidLoad];    NSArray *items = @[@"消息", @"电话" ...

  5. UISegmentedControl 的使用

    /** 设置选择器 */ - (void)setUpSegmentCtr { UISegmentedControl *segmentCtr = [[UISegmentedControl alloc] ...

  6. UISegmentedControl和UIStepper的使用

    UISegmentedControl:分栏控件,常用的属性和方法是 1.tintColor:控制分栏控件的颜色风格 2.insertSegmentWithTitle(Image):插入分栏标题(图片) ...

  7. UISegmentedControl 控件

    一.创建 UISegmentedControl* mySegmentedControl = [[UISegmentedControl alloc]initWithItems:nil]; 是不是很奇怪没 ...

  8. UI中一些不常用的控件UIActivityIndicatorView、UIProgressView、UISegmentedControl、UIStepper、UISwitch、UITextView、UIAlertController

    //UIActivityIndicatorView //小菊花,加载 #import "ActivityIndicatorVC.h" @interface ActivityIndi ...

  9. UISegmentedControl(人物简介)

    效果图 当你点击上面人物名字的时候 ,就可以随意切换人物. 这个很有趣 , 你还可以试着添加音乐播放器 .以及一些别的来完善你想做的. 好吧 , 废话不多说 , 上代码. #import " ...

  10. UILabel UISwitch UISegmentedControl UIAlertView

    基础小控件 /***************************************UIlabel*************************************/ UILabel ...

随机推荐

  1. Java代码注释XXX TODO FIXME 的意义

    特殊注释: 1 TODO 表示需要实现,但目前还未实现的功能 2 XXX 勉强可以工作,但是性能差等原因 3 FIXME 代码是错误的,不能工作,需要修复 TODO: + 说明:如果代码中有该标识,说 ...

  2. js刷新页面的几种方法

    history.go(0) location.reload() location=location location.assign(location) document.execCommand('Re ...

  3. mac 搭建APK反编译环境[转]

    APKtool 用途:获取mainifest.xml res等资源文件 下载:http://ibotpeaches.github.io/Apktool/install/ 使用:apktool d te ...

  4. BZOJ 1014: [JSOI2008]火星人prefix

    Sol Splay+Hash+二分答案. 用Splay维护Hash,二分答案判断. 复杂度 \(O(nlog^2n)\) PS:这题调了两个晚上因为没开long long.许久不写数据结构题感觉写完整 ...

  5. BZOJ 2177: 曼哈顿最小生成树

    Sol 考了好几次曼哈顿最小生成树,然而一直不会打...这次终于打出来了...神tm调试了2h...好蛋疼... 首先曼哈顿最小生成树有个结论就是讲它每45度分出一个象限,对于每个点,只与每个象限中离 ...

  6. 17.2---#字棋(CC150)

    牛客网的在线题.思路,比较简单.就是判断一下是否有连起来的1. public static boolean checkWon(int[][] board){ boolean res = false; ...

  7. Majority Number I & || && |||

    Majority Number Given an array of integers, the majority number is the number that occurs more than ...

  8. memcpy vs memmove

    [本文连接] http://www.cnblogs.com/hellogiser/p/memcpy_vs_memmove.html [分析] memcpy与memmove的目的都是将N个字节的源内存地 ...

  9. C++实现VPN工具之代码示例

    创建.连接.挂断.删除VPN实现起来并不难,下面给出一套比较完整的代码.该段代码只是示例代码,但是已经通过了编译,对API的使用和VPN操作步骤是没问题的.具体每个API代表的意义可以参看<C+ ...

  10. Zookeeper集群服务部署

    Zookeeper是一个分布式.开源的分布式应用程序协调服务,是Google的Chubby的开源实现,也是和Hadoop.Hbase相互配合的重要组件,作用就是为分布式应用程序提供一致性服务,包括配置 ...