当前位置: 代码迷 >> 综合 >> UISegmentedControl 分段控制器
  详细解决方案

UISegmentedControl 分段控制器

热度:25   发布时间:2023-12-17 01:09:23.0
  //设置分段内容NSArray *title = @[@"分组", @"全部"];UIImage *image1 = [UIImage imageNamed:@"reload_30"];UIImage *image2 = [UIImage imageNamed:@"rotate_30"];NSArray *title2 = @[image1, image2, @"文字"];UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:title2];//设置默认选中的分段control.selectedSegmentIndex = 1;control.frame = CGRectMake(0, 30, 300, 30);//替换分段内容[control setTitle:@"你好" forSegmentAtIndex:0];[control setImage:image1 forSegmentAtIndex:1];//UIControlEventValueChanged[control addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];control.tintColor = [UIColor redColor];// control.backgroundColor = [UIColor redColor];
- (void)change:(UISegmentedControl *)seg
{//获取选中的分段NSInteger index = seg.selectedSegmentIndex;//插入新的分段//[seg insertSegmentWithTitle:@"插入" atIndex:0 animated:YES];//删除原有的分段if (seg.numberOfSegments /*获取分段数*/== 1){[seg removeAllSegments]; //删除所有分段}else{//删除指定下标的分段[seg removeSegmentAtIndex:index animated:YES];}NSLog(@"seg change...%ld", index);}
  相关解决方案