UISegmentedControl的详细使用
1 NSArray *segmentedArray = [[NSArrayalloc]initWithObjects:@"one",@"two",@"three",@"four",nil];
2
3 //初始化UISegmentedControl
4 UISegmentedControl *segmentedControl = [[UISegmentedControlalloc]initWithItems:segmentedArray];
5
6 segmentedControl.frame = CGRectMake(20.0, 20.0, 250.0, 50.0);
7 //设置默认选择项索引
8 segmentedControl.selectedSegmentIndex = 2;
9 segmentedControl.tintColor = [UIColor redColor];
10
11 //设置样式
12 segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
13 UISegmentedControlStyleBordered
14 UISegmentedControlStyleBar
15 UISegmentedControlStyleBezeled
16
17 //设置在点击后是否恢复原样
18 segmentedControl.momentary = YES;
19
20 //设置指定索引的图片
21 [segmentedControl setImage:[UIImage imageNamed:@"image.png"] forSegmentAtIndex:1];
22
23 //在指定索引插入一个选项并设置图片
24 [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"image.png"] atIndex:1 animated:NO];
25
26 //移除指定索引的选项
27 [segmentedControl removeSegmentAtIndex:1 animated:NO];
28
29 //设置指定索引选项的宽度
30 [segmentedControl setWidth:70.0 forSegmentAtIndex:2];
31
32 //获取指定索引选项的标题titleForSegmentAtIndex
33 UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(100.0, 160.0, 30.0, 30.0)];
34
35 titleForSegmentAtIndex.text = [segmentedControl titleForSegmentAtIndex:0];
36
37 //设置指定索引选项不可选
38 [segmentedControl setEnabled:NO forSegmentAtIndex:1];
39
40 //判断指定索引选项是否可选
41 BOOL enableFlag = [segmentedControl isEnabledForSegmentAtIndex:1];
42
43 -(void)segmentSelected:(id)sender{
44 UISegmentedControl* control = (UISegmentedControl*)sender;
45
46 switch (control.selectedSegmentIndex) {
47 case 0:
48
49 break;
50
51 case 1:
52
53 break;
54
55 default:
56
57 break;
58 }
59
60 }