本文主要是介绍UISegmentedControl 分段控制器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//设置分段内容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);}
这篇关于UISegmentedControl 分段控制器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!