- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *segmentArray = @[
@"1",
@"2",
@"3",
@"4"
];
// 初始化UISegmentedControl
UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:segmentArray];
segmentControl.frame = CGRectMake(0, 0, self.view.frame.size.width - 20, 30);
segmentControl.center = self.view.center;
// 设置默认选择项索引
segmentControl.selectedSegmentIndex = 2;
segmentControl.tintColor = [UIColor redColor];
// 设置在点击后是否恢复原样
segmentControl.momentary = YES;
// 设置指定索引的题目
[segmentControl setTitle:@"two" forSegmentAtIndex:1];
// 设置指定索引的图片
[segmentControl setImage:[UIImage imageNamed:@"little_animal_02"] forSegmentAtIndex:3];
// 在指定索引插入一个选项并设置图片
[segmentControl insertSegmentWithImage:[UIImage imageNamed:@"little_animal_03"] atIndex:2 animated:NO];
// 在指定索引插入一个选项并设置题目
[segmentControl insertSegmentWithTitle:@"insert" atIndex:3 animated:NO];
// 移除指定索引的选项
[segmentControl removeSegmentAtIndex:0 animated:YES];
// 设置指定索引选项的宽度
[segmentControl setWidth:70.0 forSegmentAtIndex:2];
// 设置选项中图片等的左上角的位置
[segmentControl setContentOffset:CGSizeMake(10.0, 10.0) forSegmentAtIndex:4];
// 获取指定索引选项的图片imageForSegmentAtIndex
UIImageView *imageForSegmentAtIndex = [[UIImageView alloc] initWithImage:[segmentControl 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 = [segmentControl titleForSegmentAtIndex:0];
// 获取总选项数segmentedControl.numberOfSegments
UILabel *numberOfSegments = [[UILabel alloc] initWithFrame:CGRectMake(140.0, 170.0, 30.0, 30.0)];
numberOfSegments.text = [NSString stringWithFormat:@"%ld", segmentControl.numberOfSegments];
// 获取指定索引选项的宽度widthForSegmentAtIndex
UILabel *widthForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(180.0, 210.0, 70.0, 30.0)];
widthForSegmentAtIndex.text = [NSString stringWithFormat:@"%f", [segmentControl widthForSegmentAtIndex:2]];
// 设置指定索引选项不可选
[segmentControl setEnabled:NO forSegmentAtIndex:4];
// 判断指定索引选项是否可选
BOOL enableFlag = [segmentControl isEnabledForSegmentAtIndex:4];
NSLog(@"%d", enableFlag);
// 添加监听
[segmentControl addTarget:self action:@selector(didClickSegmentedControlAction:)forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentControl];
}
/**
* 监听点击了哪项
*/
- (void)didClickSegmentedControlAction:(UISegmentedControl *)segmentControl
{
NSInteger idx = segmentControl.selectedSegmentIndex;
NSLog(@"%ld", idx);
}
UISegmentedControl简单使用
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- //数组中字符串排序不区分大小写,按照数字排序 - (NSArray*) stringSortNuber:(NSA...