UISegmentedControl類似于UIButton,它可以提供多個選擇操作,響應(yīng)事件,但具有很大的局限性,我們更多的是使用自定義的,不過在這里還是介紹下它的基本用法
NSArray *segmentedArray = [[NSArrayalloc]initWithObjects:@"1",@"2",@"3",@"4",nil];
//初始化UISegmentedControl
UISegmentedControl *segmentedControl = [[UISegmentedControlalloc]initWithItems:segmentedArray];
segmentedControl.frame = CGRectMake(20.0,……)
// 設(shè)置默認選擇項索引
segmentedControl.selectedSegmentIndex = 2;
segmentedControl.tintColor = [UIColor redColor];
// 有基本四種樣式 ? ios 7 ?之后不用了
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;
// 設(shè)置在點擊后是否恢復(fù)原樣
segmentedControl.momentary = YES;
[segmentedControl setTitle:@"two" forSegmentAtIndex:1];//設(shè)置指定索引的題目
[segmentedControl setImage:[UIImage imageNamed:@"btn_jyy.png"] forSegmentAtIndex:3];//設(shè)置指定索引的圖片
// 在指定索引插入一個選項并設(shè)置圖片
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"mei.png"] atIndex:2 animated:NO];
// 在指定索引插入一個選項并設(shè)置題目
[segmentedControl insertSegmentWithTitle:@"insert" atIndex:3 animated:NO];
// 移除指定索引的選項
[segmentedControl removeSegmentAtIndex:0 animated:NO];
// 設(shè)置指定索引選項的寬度
[segmentedControl setWidth:70.0 forSegmentAtIndex:2];
// 設(shè)置選項中圖片等的左上角的位置
[segmentedControl setContentOffset:CGSizeMake(10.0,10.0) forSegmentAtIndex:4];
//獲取指定索引選項的圖片imageForSegmentAtIndex:
UIImageView *imageForSegmentAtIndex = [[UIImageViewalloc]initWithImage:[segmentedControl imageForSegmentAtIndex:1]];
imageForSegmentAtIndex.frame = CGRectMake(60.0, 120.0, 30.0, 30.0);? ;
//獲取指定索引選項的標(biāo)題titleForSegmentAtIndex
UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(100.0, 160.0, 30.0, 30.0)];
titleForSegmentAtIndex.text = [segmentedControl titleForSegmentAtIndex:0];
//獲取總選項數(shù)segmentedControl.numberOfSegments
UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(140.0, 170.0, 30.0, 30.0)];
numberOfSegments.text = [NSString stringWithFormat:@"%d",segmentedControl.numberOfSegments];
//獲取指定索引選項的寬度widthForSegmentAtIndex:
UILabel *widthForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(180.0, 210.0, 70.0, 30.0)];
widthForSegmentAtIndex.text = [NSString stringWithFormat:@"%f",[segmentedControl widthForSegmentAtIndex:2]];
// [segmentedControl setEnabled:NO forSegmentAtIndex:4];//設(shè)置指定索引選項不可選
// BOOL enableFlag = [segmentedControl isEnabledForSegmentAtIndex:4];//判斷指定索引選項是否可選
[mySegmentedControladdTarget:selfaction:@selector(didClicksegmentedControlAction:)forControlEvents:UIControlEventValueChanged];
-(void)didClicksegmentedControlAction:(UISegmentedControl *)Seg{
NSInteger Index = Seg.selectedSegmentIndex;
......}