美文网首页
UISegmentedControl选择控件

UISegmentedControl选择控件

作者: 俊俊吖 | 来源:发表于2017-01-19 15:50 被阅读0次

-(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;

}

效果如下

相关文章

网友评论

      本文标题:UISegmentedControl选择控件

      本文链接:https://www.haomeiwen.com/subject/fzlbmttx.html