@interface ViewController (){ UISegmentedControl *_segment;}
@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; /* //Items是数组,按顺序添加到segment(分段)上 UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[ [UIImage imageNamed:@"1.jpg"], @"电话",@"消息"]]; //默认选择的 segment.selectedSegmentIndex = 1; segment.frame = CGRectMake(100.f, 100.f, 200.f, 30.f); segment.tintColor = [UIColor orangeColor]; [self.view addSubview:segment]; */ _segment = [[UISegmentedControl alloc] initWithItems:@[@"河科大",@"清华",@"北大"]]; _segment.frame = CGRectMake(100.f, 100.f, 200.f, 30.f); //这个atIndex中又容错性,用100也是加到第四个,自己设计的时候,如果加入的索引值大于数组元素个数,酒吧索引值设置为数组长度 [_segment insertSegmentWithImage:[UIImage imageNamed:@"1.jpg"] atIndex:1 animated:YES]; [_segment removeSegmentAtIndex:1 animated:YES]; //设置宽度 [_segment setWidth:33.f forSegmentAtIndex:1]; //设置不能点击 [_segment setEnabled:NO forSegmentAtIndex:1]; [self.view addSubview:_segment]; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10.f, 20.f, 30.f, 30.f)]; btn.backgroundColor = [UIColor orangeColor]; [self.view addSubview:btn]; [btn addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside]; UIButton *delBtn = [[UIButton alloc] initWithFrame:CGRectMake(40.f, 20.f, 30.f, 30.f)]; delBtn.backgroundColor = [UIColor purpleColor]; [delBtn addTarget:self action:@selector(delBtnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:delBtn]; }- (void)action{ //点击按钮添加一个段// [_segment insertSegmentWithImage:[UIImage imageNamed:@"1.jpg"] atIndex:4 animated:YES]; [_segment insertSegmentWithTitle:@"北航" atIndex:1 animated:YES];}- (void)delBtnClick{ //删除 [_segment removeSegmentAtIndex:1 animated:YES];}