iOS - UISegmentedControl相关

- (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 addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    //设置指定索引的题目   
    [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);  
      
    [self.view addSubview:widthForSegmentAtIndex];  
    [self.view addSubview:numberOfSegments];  
    [self.view addSubview:titleForSegmentAtIndex];  
    [self.view addSubview:imageForSegmentAtIndex];  
    [self.view addSubview:segmentedControl];  
      
    //移除所有选项  
    //[segmentedControl removeAllSegments];  

    [super viewDidLoad];  
}  

修改字体的大小和颜色

NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName,  [UIFont systemFontOfSize:17],NSFontAttributeName, nil];
    
[self.segment setTitleTextAttributes:dic forState:UIControlStateNormal];

更改segment的item标题,思路:先删除所有items,再插入item

[segment removeAllSegments];
[segment insertSegmentWithTitle:@"拍照" atIndex:0 animated:NO];

你可能感兴趣的:(iOS - UISegmentedControl相关)