-(void)Uiconfig{
NSArray*arr = [[NSArrayalloc]initWithObjects:@"学生",@"老师",nil];
//先创建一个数组用于设置标题
UISegmentedControl*segment = [[UISegmentedControlalloc]initWithItems:arr];
//在没有设置[segment setApportionsSegmentWidthsByContent:YES]时,每个的宽度按segment的宽度平分
[segmentsetTintColor:[UIColorcolorWithHexString:@"#ff6121"]];//设置segments的颜色
segment.selectedSegmentIndex=0;
segment.frame=CGRectMake(0,0,100,30);
self.navigationItem.titleView= segment;
[segmentaddTarget:selfaction:@selector(change:)forControlEvents:UIControlEventValueChanged];
//当选中不同的segment时,会执行change:方法
//设置frame
}
-(void)change:(id)sender{
UISegmentedControl* control = (UISegmentedControl*)sender;
switch(control.selectedSegmentIndex) {
case0:
DLog(@"学生");
break;
case1:
DLog(@"老师");
break;
default:
break;
}
效果如下