UISegmentedControl(转)
初始化UISegmentedControl
NSArray *arr = [[NSArray alloc]initWithObjects:@"轻拍",@"长按",@"清扫",@"旋转",@"捏合",@"拖拽", nil];
//先创建一个数组用于设置标题
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:arr];
//在没有设置[segment setApportionsSegmentWidthsByContent:YES]时,每个的宽度按segment的宽度平分
segment.frame = CGRectMake(0, 400, 320, 40);
//设置frame
管理Segment的内容
[segment setTitle:@"大便" forSegmentAtIndex:3];
//设置下标为3的segment的标题 下标以0开始 IOS里的所有下标都是以0开始
[segment setImage:[UIImage imageNamed:@"3"] forSegmentAtIndex:4];
//设置下标为4的segment的图片
管理Segments
[segment insertSegmentWithImage:[UIImage imageNamed:@"3"] atIndex:2 animated:YES]; //设置图片
[segment insertSegmentWithTitle:@"ddd" atIndex:0 animated:YES];//设置标题
[segment numberOfSegments];//得到segment的数量
[segment removeAllSegments];//移出所有segment
[segment removeSegmentAtIndex:2 animated:YES];//移出下标为2的segment
segment.selectedSegmentIndex = 0;//选中第几个segment 一般用于初始化时选中
管理Segment的行为和外观
segment.momentary = NO;
//默认为NO 当设置为YES时,当被选中后,一会儿后不显示被选中状态(最左边的样子),不选中状态即下图的右边几个按钮一样

segment.segmentedControlStyle = UISegmentedControlStyleBar;
typedef enum {
UISegmentedControlStylePlain,
UISegmentedControlStyleBordered,
UISegmentedControlStyleBar,
UISegmentedControlStyleBezeled,
} UISegmentedControlStyle;
//设置样式 当为Bordered和Bar时tintColor才有效 下图为tintcolor为红色时的样子
//Bar样式
//Bordered
[segment setEnabled:NO]; //设置segment是否可用 此方法是其父类UIControl的方法
[segment setEnabled:NO forSegmentAtIndex:2];//设置下标为2的segment不可用
[segment setWidth:100 forSegmentAtIndex:2]; //这时下表为2的segment的宽度
[segment setContentOffset:CGSizeMake(10, 10) forSegmentAtIndex:2];//设置内容偏移
segment.apportionsSegmentWidthsByContent = YES; //是否根据segment的内容改变segment的宽度
自定义外观
[segment setTintColor:[UIColor redColor]]; //设置segments的颜色
[segment setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
typedef enum {
UIBarMetricsDefault, //竖屏
UIBarMetricsLandscapePhone, 横屏
} UIBarMetrics;
//设置在某个状态下segments的背景图片
[segment setTitleTextAttributes:dic forState:UIControlStateNormal];
NSString *const UITextAttributeFont; value: UIFont
NSString *const UITextAttributeTextColor; value: UIColor
NSString *const UITextAttributeTextShadowColor; value: UIColor
NSString *const UITextAttributeTextShadowOffset; value: NSValue wrapping a UIOffset
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],UITextAttributeTextColor,[UIFont fontWithName:@"SnellRoundhand-Bold" size:24],UITextAttributeFont ,nil];
//设置标题的颜色 字体和大小 阴影和阴影颜色
[segment addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
//当选中不同的segment时,会执行change:方法
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 ...
随机推荐
- 真不知道JavaScrip【数组】还有这么多东西....
前段时间在频繁的用数组,但一直不知道JavaScript 数组还有这么多东西,收集了一下看看: 首先:数组是对象的特殊形式,接下来看看它有哪些方法.....push()在末尾增加一个或者是多个 uns ...
- LeetCode Binary Tree Upside Down
原题链接在这里:https://leetcode.com/problems/binary-tree-upside-down/ Given a binary tree where all the rig ...
- python中新式类和经典类的区别
1).python在类中的定义在py2-3版本上是使用的有新式类和经典类两种情况,在新式类和经典类的定义中最主要的区别是在定义类的时候是否出现引用object;如:经典类:Class 类名::而新式类 ...
- Inside Kolla - 04 Kolla 目录结构
Kolla 目录结构 把 Kolla 的源代码下载下来后,先从总体上分析 Kolla 的目录结构,查看顶层目录结构,使用 tree -L 1 输出 . ├── ansible ├── compose ...
- 九个uname命令获取Linux系统详情的实例
当你在控制台模式下,无法通过“鼠标右键 > 关于”获取操作系统的信息.这时,在Linux下,你可以使用uname命令,帮助你完成这些工作. Uname是unix name的缩写.在控制台中实际使 ...
- EF Code First教程-01 创建一个简单的Code First程序
1 从nuget中搜索并添加EF 2 在app.config或web.config中添加数据库连接 <connectionStrings> <add name="conns ...
- tableview head
http://stackoverflow.com/questions/18880341/why-is-there-extra-padding-at-the-top-of-my-uitableview- ...
- C# WPF定时器
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来.我们都知道计算机技术发展日新月异,速度惊人的快,你我稍不留神,就会被慢慢淘汰!因此:每日不间断的学习是避免被 ...
- The Struts dispatcher cannot be found. This is usually caused by using Strut
The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the assoc ...
- 一款不错的多选下拉列表利器—— Ext.ux.form.SuperBoxSelect
在B/S系统中,下拉列表(select/dropdownlist/combobox)的应用随处可见,为了增强用户体验,开发人员也常常会做一些带联想功能的下拉列表, 特别是数据项比较多的时候,用户筛选起 ...