UISegmentControl的使用方法


 NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",nil];  
//初始化UISegmentedControl
UISegmentedControl *segmentedTemp = [[UISegmentedControl alloc]initWithItems:segmentedArray];
segmentedControl = segmentedTemp;
segmentedControl.frame = CGRectMake(60.0, 10.0, 200.0, 50.0);

[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];//判斷指定索引選項是否可選

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com