可以直接复制使用
#import "FirstViewController.h"
#import "Masonry.h" @interface FirstViewController () @property (nonatomic, strong) UISegmentedControl * segmentedControl_one; @end @implementation FirstViewController #pragma mark - 生命周期
#pragma mark viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad]; [self basicSetting]; [self addSegmentedControl_one]; [self insertOrDeleteInSegmendtedControl]; [self settingSegmentProperty]; [self obtainSectionItem];
} #pragma mark - 系统代理 #pragma mark - 点击事件
- (void)segmentedControl_one:(UISegmentedControl *)sender
{
NSLog(@"index: %ld",(long)sender.selectedSegmentIndex); } #pragma mark - 实现方法
#pragma mark 基本设置
- (void)basicSetting
{
self.title = @"基本概念";
} - (void)addSegmentedControl_one
{
[self.view addSubview:self.segmentedControl_one];
[self.segmentedControl_one mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.view).with.offset();
make.right.mas_equalTo(self.view).with.offset(-);
make.top.mas_equalTo(self.view).with.offset();
make.height.mas_equalTo();
}];
} - (void)insertOrDeleteInSegmendtedControl
{
// 插入一段文字
[self.segmentedControl_one insertSegmentWithTitle:@"插入的元素" atIndex: animated:YES]; // 插入图片
[self.segmentedControl_one insertSegmentWithImage:[[UIImage imageNamed:@""] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] atIndex: animated:YES]; // 删除
[self.segmentedControl_one removeSegmentAtIndex: animated:YES]; // 根据下标修改分段标题
[self.segmentedControl_one setTitle:@"修改的分段标题" forSegmentAtIndex:];
} - (void)settingSegmentProperty
{
// 设置段落的宽度
[self.segmentedControl_one setWidth:40.0f forSegmentAtIndex:]; // 设置分段中标题的位置 (0,0)是中心点的位置
[self.segmentedControl_one setContentOffset:CGSizeMake(, ) forSegmentAtIndex:];
} - (void)obtainSectionItem
{
//
//获取指定索引选项的图片 imageForSegmentAtIndex:
UIImageView *imageForSegmentAtIndex = [[UIImageView alloc]initWithImage:[self.segmentedControl_one imageForSegmentAtIndex:]];
imageForSegmentAtIndex.frame =CGRectMake(60.0,100.0,30.0,30.0); //获取指定索引选项的标题 titleForSegmentAtIndex
UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(60.0,200.0,200.0,30.0)];
titleForSegmentAtIndex.text = [self.segmentedControl_one titleForSegmentAtIndex:]; //获取总选项数 segmentedControl.numberOfSegments
UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(60.0,300.0,30.0,200.0)];
numberOfSegments.text = [NSString stringWithFormat:@"%lu",(unsigned long)self.segmentedControl_one.numberOfSegments]; //获取指定索引选项的宽度 widthForSegmentAtIndex:
UILabel *widthForSegmentAtIndex = [[UILabel alloc] initWithFrame:CGRectMake(40.0,400.0,200.0,30.0)];
widthForSegmentAtIndex.text = [NSString stringWithFormat:@"宽度: %f",[self.segmentedControl_one widthForSegmentAtIndex:]]; //设置默认选择项索引
self.segmentedControl_one.selectedSegmentIndex =;
// 设置色彩
self.segmentedControl_one.tintColor = [UIColor redColor]; self.segmentedControl_one.momentary = NO;//设置在点击后是否恢复原样 [self.segmentedControl_one setEnabled:NO forSegmentAtIndex:];//设置指定索引选项不可选
BOOL enableFlag = [self.segmentedControl_one isEnabledForSegmentAtIndex:];//判断指定索引选项是否可选
NSLog(@"enableFlag: %d",enableFlag); [self.view addSubview:widthForSegmentAtIndex];
[self.view addSubview:numberOfSegments];
[self.view addSubview:titleForSegmentAtIndex];
[self.view addSubview:imageForSegmentAtIndex];
[self.view addSubview:self.segmentedControl_one];
} #pragma mark - setter & getter - (UISegmentedControl *)segmentedControl_one
{
if (!_segmentedControl_one)
{
NSArray * array = @[@"第一段",@"第二段",@"第三段",@"第四段"];
self.segmentedControl_one = [[UISegmentedControl alloc] initWithItems:array]; [self.segmentedControl_one addTarget:self action:@selector(segmentedControl_one:) forControlEvents:UIControlEventValueChanged];
}
return _segmentedControl_one;
} @end