//首先把你要添加的控件内的文字放入一个数组
NSArray * name = @[ @"就诊提醒",@"就诊记录"];
//开辟一个新的UISegmentedControl 空间 并且把名字数组放进去
UISegmentedControl * segmented = [[UISegmentedControl alloc]initWithItems:name];
//设置控件的大小
segmented.frame = CGRectMake(0, 0, 440, 60);
//设置控件的中心位置
segmented.center = CGPointMake(200, 110);
//设置控件默认选中的位置
segmented.selectedSegmentIndex = 0;
//设置背景颜色
segmented.tintColor = [UIColor whiteColor];
//设置字体颜色
[segmented setTintColor:[UIColor orangeColor]];
//设置字体大小
UIFont *font = [UIFont boldSystemFontOfSize:24];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
[segmented setTitleTextAttributes:attributes forState:UIControlStateNormal];
//添加到视图上
[self.view addSubview:segmented];
//添加点击事件
[segmented addTarget:self action:@selector(segmentedValueChanged:) forControlEvents:UIControlEventValueChanged];
//点击事件的实现
- (void)segmentedValueChanged:(UISegmentedControl *)sengder
{
switch (sengder.selectedSegmentIndex) {
case 0:
self.view.backgroundColor = [UIColor blueColor];
break;
case 1:
self.view.backgroundColor = [UIColor yellowColor];
default:
break;
}
}
//实现效果