您的位置:首页 > 产品设计 > UI/UE

UISegmentedControl 分段控制器

2015-03-05 15:21 246 查看
//设置分段内容
NSArray *title = @[@"分组", @"全部"];

UIImage *image1 = [UIImage imageNamed:@"reload_30"];
UIImage *image2 = [UIImage imageNamed:@"rotate_30"];
NSArray *title2 = @[image1, image2, @"文字"];

UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:title2];

//设置默认选中的分段
control.selectedSegmentIndex = 1;

control.frame = CGRectMake(0, 30, 300, 30);

//替换分段内容
[control setTitle:@"你好" forSegmentAtIndex:0];
[control setImage:image1 forSegmentAtIndex:1];

//UIControlEventValueChanged
[control addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];

control.tintColor = [UIColor redColor];

//    control.backgroundColor = [UIColor redColor];


- (void)change:(UISegmentedControl *)seg
{
//获取选中的分段
NSInteger index = seg.selectedSegmentIndex;
//插入新的分段
//[seg insertSegmentWithTitle:@"插入" atIndex:0 animated:YES];
//删除原有的分段
if (seg.numberOfSegments /*获取分段数*/== 1)
{
[seg removeAllSegments]; //删除所有分段
}
else
{
//删除指定下标的分段
[seg removeSegmentAtIndex:index animated:YES];
}

NSLog(@"seg change...%ld", index);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: