如上图: 分两步:
一、创建320长度背景,高不超过40最好
[php] view plaincopy
- //segment背景图
- UIImageView *segBgImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"segmented_bg.png"]];
- [segBgImg setUserInteractionEnabled:YES];
- [segBgImg setFrame:CGRectMake(0, 44, 320, 38)];
- //segment实例对象及初始化
- UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:nil];
- segment.selectedSegmentIndex = 0;
- [segment insertSegmentWithImage:[UIImage imageNamed:@"segmented_price_all_selected.png"] atIndex:0 animated:NO];
- [segment insertSegmentWithImage:[UIImage imageNamed:@"segmented_price_free.png"] atIndex:1 animated:NO];
- segment.frame = CGRectMake(10, 5, 184, 28);
- [segment setMomentary:YES];
- [segment addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged];
- [segBgImg addSubview:segment];
- [segment release];
- [self.view addSubview:segBgImg];
- [segBgImg release];
二、segmentClick相应事件中选中当前焦点
[php] view plaincopy
- //选择时触发
- - (void)segmentClick:(UISegmentedControl *)segment
- {
- NSInteger index = segment.selectedSegmentIndex;
- switch (index) {
- case 0: //全部
- [segment setImage:[UIImage imageNamed:@"segmented_price_all_selected.png"] forSegmentAtIndex:0];
- [segment setImage:[UIImage imageNamed:@"segmented_price_free.png"] forSegmentAtIndex:1];
- break;
- case 1: //免费
- [segment setImage:[UIImage imageNamed:@"segmented_price_all.png"] forSegmentAtIndex:0];
- [segment setImage:[UIImage imageNamed:@"segmented_price_free_selected.png"] forSegmentAtIndex:1];
- break;
- }
- }
- - (void)viewDidLoad {
- NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",nil];
- //初始化UISegmentedControl
- UISegmentedControl * segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
- segmentedControl.frame = CGRectMake(60.0, 10.0, 200.0, 50.0);
- segmentedControl.segmentedControlStyle = 2;
- [segmentedControl setTitle:@"two" forSegmentAtIndex:1];//设置指定索引的题目
- [segmentedControl setImage:[UIImage imageNamed:@"lan.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:1];//设置选项中图片等的左上角的位置
- //获取指定索引选项的图片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]];
- segmentedControl.selectedSegmentIndex = 2;//设置默认选择项索引
- segmentedControl.tintColor = [UIColor redColor];
- segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;//设置样式
- segmentedControl.momentary = YES;//设置在点击后是否恢复原样
- [segmentedControl setEnabled:NO forSegmentAtIndex:4];//设置指定索引选项不可选
- BOOL enableFlag = [segmentedControl isEnabledForSegmentAtIndex:4];//判断指定索引选项是否可选
- NSLog(@"%d",enableFlag);
- }
注: 多个segment切换时用for循环处理一下即可
时间: 2024-10-01 21:38:04