UISegmentedControl是iOS中的分段控件。
每个segment都能被点击,相当于集成了若干个button。
通常我们会点击不同的segment来切换不同的view。
segment 意思是片段,个人认为敲代码如果有一些词汇基础的话会非常方便,想想看如果一段代码,或者代码的文档上所有的单词都认识会是一件多么爽的事…..
闲话不说,上代码看用法
- (void)segment{
UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"1",@"2",@"3",@"4"]];
segment.frame = CGRectMake(50, 50, 200, 50);
segment.backgroundColor = [UIColor cyanColor];
[self addSubview:self.button];
}
//设置边框,字体 和选中颜色
segment.tintColor = [UIColor redColor];
//设置是否有被选中的效果
segment.momentary = YES;
//读者可自行演示效果
//在某个位置添加片段 Index 是从左向右 从零开始
[segment insertSegmentWithTitle:@"AAA" atIndex:1 animated:YES];
//在某个位置删除片段 Index 是从左向右 从零开始
// [segment removeSegmentAtIndex:1 animated:YES];
//插入图片
[segment insertSegmentWithImage:[[UIImage imageNamed:@"football.jpg"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] atIndex:1 animated:YES];
//获取抬头
NSLog(@"%@",[segment titleForSegmentAtIndex:0]);
2015-10-20 10:17:51.487 UIControl 及其⼦子类[1581:70803] 1
//设置某个位置的部分宽
[segment setWidth:100.0 forSegmentAtIndex:0];
[segment addTarget:self action:@selector(segmentAction:) forControlEvents:(UIControlEventValueChanged)];
如何添加方法
[segment addTarget:self action:@selector(segmentAction) forControlEvents:(UIControlEventValueChanged)];
- (void)segmentAction{
if (self.views.segment.selectedSegmentIndex == 0) {
.......
}else if (self.views.segment.selectedSegmentIndex == 1){
.......
}else{
.......
}
}