-(void)Uiconfig{
NSArray*arr = [[NSArrayalloc]initWithObjects:@"學(xué)生",@"老師",nil];
//先創(chuàng)建一個(gè)數(shù)組用于設(shè)置標(biāo)題
UISegmentedControl*segment = [[UISegmentedControlalloc]initWithItems:arr];
//在沒(méi)有設(shè)置[segment setApportionsSegmentWidthsByContent:YES]時(shí)韭脊,每個(gè)的寬度按segment的寬度平分
[segmentsetTintColor:[UIColorcolorWithHexString:@"#ff6121"]];//設(shè)置segments的顏色
segment.selectedSegmentIndex=0;
segment.frame=CGRectMake(0,0,100,30);
self.navigationItem.titleView= segment;
[segmentaddTarget:selfaction:@selector(change:)forControlEvents:UIControlEventValueChanged];
//當(dāng)選中不同的segment時(shí)妓灌,會(huì)執(zhí)行change:方法
//設(shè)置frame
}
-(void)change:(id)sender{
UISegmentedControl* control = (UISegmentedControl*)sender;
switch(control.selectedSegmentIndex) {
case0:
DLog(@"學(xué)生");
break;
case1:
DLog(@"老師");
break;
default:
break;
}
效果如下