记录
//初始化一个UISegmentedControl控件test
UISegmentedControl *test = [[UISegmentedControl alloc]initWithItems:@[@"一",@"二",@"三",@"四",@"五"]];
test.frame = CGRectMake(0, 200, 400, 50);
//设置无边框分段控制器
//设置test控件的颜色为透明
test.tintColor = [UIColor clearColor];
//定义选中状态的样式selected,类型为字典
NSDictionary *selected = @{NSFontAttributeName:[UIFont systemFontOfSize:20],
NSForegroundColorAttributeName:[UIColor redColor]};
//定义未选中状态下的样式normal,类型为字典
NSDictionary *normal = @{NSFontAttributeName:[UIFont systemFontOfSize:10],
NSForegroundColorAttributeName:[UIColor blackColor]};
/*
NSFontAttributeName -> 系统宏定义的特殊键,用来给格式字典中的字体赋值
NSForegroundColorAttributeName -> 系统宏定义的特殊键,用来给格式字典中的字体颜色赋值
*/
//通过setTitleTextAttributes: forState: 方法来给test控件设置文字内容的格式
[test setTitleTextAttributes:normal forState:UIControlStateNormal];
[test setTitleTextAttributes:selected forState:UIControlStateSelected];
//设置test初始状态下的选中下标
test.selectedSegmentIndex = 0;
//将test添加到某个View上
[self.view addSubview:test];
屏幕快照 2017-11-07 上午10.57.01.png