测试已测试 | ✗ |
Lang语言 | Obj-CObjective C |
许可证 | MIT |
发布最新发布 | 2014年12月 |
由 Elliott Minns 维护。
为iOS开发的纵向分段控制视图
您需要做的就是将'EMVerticalSegmentedControl'文件拖到您的项目中。
使用Interface Builder,只需将指定的视图类设置为EMVerticalSegmentedControl,或手动用如下代码创建:
EMVerticalSegmentedControl *control = [[EMVerticalSegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 150, 140)];
control.sectionTitles = @[@"On", @"Off", @"Maybe", @"Could you repeat?"];
[self.view addSubview:control];
control.center = CGPointMake(320 / 2, 568 / 2);
确保设置了sectionTitles,否则您将得到一个空白的段 :)
要监听段的变化,您有两种选择。第一种是实现经典的目标方法
- (void)viewDidLoad {
...
[control addTarget:self action:@selector(segmentDidChange:) forControlEvents:UIControlEventValueChanged];
}
- (void)segmentDidChange:(id)sender {
NSInteger index = ((EMVerticalSegmentedControl *)sender).selectedSegmentIndex;
NSLog(@"Value changed selector: %lu", index);
}
您也可以添加一个用于回调的block对象。
- (void)viewDidLoad {
[control setIndexChangeBlock:^(NSInteger index) {
NSLog(@"Value changed in block: %lu", index);
}];
}
这两者不是互斥的,如果需要,您可以使用两者。
NSArray *sectionTitles - 要作为对应段落索引标题显示的NSString数组。
NSTextAlignment textAlignment - 用于设置标题文本的对齐方式。
UIFont *titleFont - 用来绘制标题的字体。
UIColor *selectedTextColor - 已选择段的标题颜色。
UIColor *textColor - 未选择段的标题颜色。
UIColor *selectedSegmentColor - 已选择段背景颜色。
UIColor *backgroundColor - 未选择段背景颜色。
CGFloat cornerRadius - 整个视图的圆角半径。
NSInteger selectedSegmentIndex - 当前选择的段。