//创建segmentControl 分段控件
UISegmentedControl *segC = [[UISegmentedControl alloc]initWithFrame:CGRectMake(50, 100, 200, 30)];
//添加小按钮
[segC insertSegmentWithTitle:@"左边" atIndex:0 animated:YES];
[segC insertSegmentWithTitle:@"中间" atIndex:1 animated:YES];
[segC insertSegmentWithTitle:@"右边" atIndex:2 animated:YES];
//设置样式
[segC setTintColor:[UIColor grayColor]];
//self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
// self.segmentedControl.tintColor = [UIColor whiteColor];
//设置字体样式
[segC setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];
//添加事件
[segC addTarget:self action:@selector(segCChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segC];
}
-(void)segCChanged:(UISegmentedControl *)seg
{
NSInteger i = seg.selectedSegmentIndex;
NSLog(@"切换了状态 %lu",i);
}