UISegmentControl
2015-08-04 00:09
513 查看
本文主要介绍UISegmentControl的用法, 见代码
//做一个数组标题, 用于初始化方法中
NSArray *segArr = @[@"第1个", @"第2个", @"第3个"];
//用一个不可变数组, 初始化一个可变数组
self.titleArr = [NSMutableArray arrayWithArray:segArr];
//创建一个segmentControl, 这里传入的数组既可以是可变数组, 也可以是不可变数组.
//今后遇到传入默认的字典或者数组是不可变的, 也可以传入可变的.
UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:self.titleArr];
segmentControl.frame = CGRectMake(0, 30, kScreenWidth, 40);
segmentControl.tag = 1;
//添加点击事件, 点击时是索引值的变化
[segmentControl addTarget:self action:@selector(actionSegmentedControl:) forControlEvents:(UIControlEventValueChanged)];
//设置默认点击位置
segmentControl.selectedSegmentIndex = 1;
//设置填充颜色(选中时的颜色)
segmentControl.tintColor = [UIColor purpleColor];
//设置索引处的宽度, 这里宽度改为80
[segmentControl setWidth:120 forSegmentAtIndex:0];
//添加一个新的segment(记为插入一个), 添加的segment放在什么位置, 就让segment的index为几.(这里想放在第2个和第2个之间)
[segmentControl insertSegmentWithTitle:@"第4个" atIndex:2 animated:YES];
//删除一个segment
[segmentControl removeSegmentAtIndex:2 animated:YES];
//改变字体的位置
//[segmentControl setContentOffset:CGSizeMake(5.0, -5) forSegmentAtIndex:1];
//设置点击后是否恢复原样, 默认是No 不恢复
segmentControl.momentary = NO;
//获取制定索引上的文字
NSLog(@"%@", [segmentControl titleForSegmentAtIndex:2]);
//获取索引的个数
NSLog(@"%lu", segmentControl.numberOfSegments);
//获取指定下标的宽度
NSLog(@"%f", [segmentControl widthForSegmentAtIndex:1]);
[self.view addSubview:segmentControl];
[segmentControl release];
下面根据上述代码, 完成一个小程序. 点击按钮添加或者删除segment 不同的segment的view不同
UIButton *removeButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
removeButton.backgroundColor = [UIColor purpleColor];
removeButton.frame = CGRectMake(40, 90, 100, 40);
[removeButton setTitle:@"删除" forState:(UIControlStateNormal)];
[removeButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
[removeButton setTitle:@"删除" forState:(UIControlStateHighlighted)];
[removeButton setTitleColor:[UIColor grayColor] forState:(UIControlStateHighlighted)];
[removeButton addTarget:self action:@selector(removeButtonOnClick:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:removeButton];
UIButton *addButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
addButton.backgroundColor = [UIColor purpleColor];
addButton.frame = CGRectMake(kScreenWidth - 120, 90, 100, 40);
[addButton setTitle:@"添加" forState:(UIControlStateNormal)];
[addButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
[addButton setTitle:@"添加" forState:(UIControlStateHighlighted)];
[addButton setTitleColor:[UIColor grayColor] forState:(UIControlStateHighlighted)];
[addButton addTarget:self action:@selector(addButtonOnClick:) forControlEvents:(UIControlEventTouchUpInside)];
//指定下标是否可选
NSLog(@"%d", [segmentControl isEnabledForSegmentAtIndex:1]);
//设置索引项不可选
//[segmentControl setEnabled:NO forSegmentAtIndex:2];
//移除全部项
//[segmentControl removeAllSegments];
[self.view addSubview:addButton];
// self.view1 = [[UIView alloc] initWithFrame:(CGRectMake(0, 160, kScreenWidth, kScreenHeight - 160))];
// self.view1.backgroundColor = [UIColor yellowColor];
// [self.view addSubview:self.view1];
// [self.view1 release];
//
// self.view2 = [[UIView alloc] initWithFrame:(CGRectMake(0, 160, kScreenWidth, kScreenHeight - 160))];
// self.view2.backgroundColor = [UIColor orangeColor];
// [self.view addSubview:self.view2];
// [self.view2 release];
//
// self.view3 = [[UIView alloc] initWithFrame:(CGRectMake(0, 160, kScreenWidth, kScreenHeight - 160))];
// self.view3.backgroundColor = [UIColor purpleColor];
// [self.view addSubview:self.view3];
// [self.view3 release];
for (int i = 0; i < 3; i++) {
self.view2 = [[UIView alloc] initWithFrame:(CGRectMake(0, 160, kScreenWidth, kScreenHeight - 160))];
//self.view1.backgroundColor = [UIColor yellowColor];
self.view2.tag = 2 + i;
self.view2.hidden = YES;
CGFloat red = arc4random()%256 / 255.0;
CGFloat green = arc4random()%256 / 255.0;
CGFloat blue = arc4random()%256 / 255.0;
self.view2.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];
[self.view addSubview:self.view2];
[self.view2 release];
}
[segmentControl release], segmentControl = nil;
//做一个数组标题, 用于初始化方法中
NSArray *segArr = @[@"第1个", @"第2个", @"第3个"];
//用一个不可变数组, 初始化一个可变数组
self.titleArr = [NSMutableArray arrayWithArray:segArr];
//创建一个segmentControl, 这里传入的数组既可以是可变数组, 也可以是不可变数组.
//今后遇到传入默认的字典或者数组是不可变的, 也可以传入可变的.
UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:self.titleArr];
segmentControl.frame = CGRectMake(0, 30, kScreenWidth, 40);
segmentControl.tag = 1;
//添加点击事件, 点击时是索引值的变化
[segmentControl addTarget:self action:@selector(actionSegmentedControl:) forControlEvents:(UIControlEventValueChanged)];
//设置默认点击位置
segmentControl.selectedSegmentIndex = 1;
//设置填充颜色(选中时的颜色)
segmentControl.tintColor = [UIColor purpleColor];
//设置索引处的宽度, 这里宽度改为80
[segmentControl setWidth:120 forSegmentAtIndex:0];
//添加一个新的segment(记为插入一个), 添加的segment放在什么位置, 就让segment的index为几.(这里想放在第2个和第2个之间)
[segmentControl insertSegmentWithTitle:@"第4个" atIndex:2 animated:YES];
//删除一个segment
[segmentControl removeSegmentAtIndex:2 animated:YES];
//改变字体的位置
//[segmentControl setContentOffset:CGSizeMake(5.0, -5) forSegmentAtIndex:1];
//设置点击后是否恢复原样, 默认是No 不恢复
segmentControl.momentary = NO;
//获取制定索引上的文字
NSLog(@"%@", [segmentControl titleForSegmentAtIndex:2]);
//获取索引的个数
NSLog(@"%lu", segmentControl.numberOfSegments);
//获取指定下标的宽度
NSLog(@"%f", [segmentControl widthForSegmentAtIndex:1]);
[self.view addSubview:segmentControl];
[segmentControl release];
下面根据上述代码, 完成一个小程序. 点击按钮添加或者删除segment 不同的segment的view不同
UIButton *removeButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
removeButton.backgroundColor = [UIColor purpleColor];
removeButton.frame = CGRectMake(40, 90, 100, 40);
[removeButton setTitle:@"删除" forState:(UIControlStateNormal)];
[removeButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
[removeButton setTitle:@"删除" forState:(UIControlStateHighlighted)];
[removeButton setTitleColor:[UIColor grayColor] forState:(UIControlStateHighlighted)];
[removeButton addTarget:self action:@selector(removeButtonOnClick:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:removeButton];
UIButton *addButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
addButton.backgroundColor = [UIColor purpleColor];
addButton.frame = CGRectMake(kScreenWidth - 120, 90, 100, 40);
[addButton setTitle:@"添加" forState:(UIControlStateNormal)];
[addButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
[addButton setTitle:@"添加" forState:(UIControlStateHighlighted)];
[addButton setTitleColor:[UIColor grayColor] forState:(UIControlStateHighlighted)];
[addButton addTarget:self action:@selector(addButtonOnClick:) forControlEvents:(UIControlEventTouchUpInside)];
//指定下标是否可选
NSLog(@"%d", [segmentControl isEnabledForSegmentAtIndex:1]);
//设置索引项不可选
//[segmentControl setEnabled:NO forSegmentAtIndex:2];
//移除全部项
//[segmentControl removeAllSegments];
[self.view addSubview:addButton];
// self.view1 = [[UIView alloc] initWithFrame:(CGRectMake(0, 160, kScreenWidth, kScreenHeight - 160))];
// self.view1.backgroundColor = [UIColor yellowColor];
// [self.view addSubview:self.view1];
// [self.view1 release];
//
// self.view2 = [[UIView alloc] initWithFrame:(CGRectMake(0, 160, kScreenWidth, kScreenHeight - 160))];
// self.view2.backgroundColor = [UIColor orangeColor];
// [self.view addSubview:self.view2];
// [self.view2 release];
//
// self.view3 = [[UIView alloc] initWithFrame:(CGRectMake(0, 160, kScreenWidth, kScreenHeight - 160))];
// self.view3.backgroundColor = [UIColor purpleColor];
// [self.view addSubview:self.view3];
// [self.view3 release];
for (int i = 0; i < 3; i++) {
self.view2 = [[UIView alloc] initWithFrame:(CGRectMake(0, 160, kScreenWidth, kScreenHeight - 160))];
//self.view1.backgroundColor = [UIColor yellowColor];
self.view2.tag = 2 + i;
self.view2.hidden = YES;
CGFloat red = arc4random()%256 / 255.0;
CGFloat green = arc4random()%256 / 255.0;
CGFloat blue = arc4random()%256 / 255.0;
self.view2.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];
[self.view addSubview:self.view2];
[self.view2 release];
}
[segmentControl release], segmentControl = nil;
- (void)changeView:(UISegmentedControl *)segmentControl { for (int i = 0; i < self.titleArr.count; i++) { segmentControl = (UISegmentedControl *)[self.view viewWithTag:1]; UIView *view = [self.view viewWithTag:i + 2]; if (segmentControl.selectedSegmentIndex == i) { view.hidden = NO; } else { view.hidden = YES; } } } #pragma mark 实现点击方法 - (void)actionSegmentedControl:(UISegmentedControl *)segmentControl { //switch (segmentControl.selectedSegmentIndex) { [self changeView:segmentControl]; // case 0: // NSLog(@"第1项被选中"); // self.view1.hidden = NO; // self.view2.hidden = YES; // self.view3.hidden = YES; // // break; // case 1: // NSLog(@"第2项被选中"); // self.view1.hidden = YES; // self.view2.hidden = NO; // self.view3.hidden = YES; // break; // case 2: // NSLog(@"第3项被选中"); // self.view1.hidden = YES; // self.view2.hidden = YES; // self.view3.hidden = NO; // break; // default: // break; // } } #pragma mark 添加 - (void)addButtonOnClick:(UIButton *)button { UISegmentedControl *segmentControl = (UISegmentedControl *)[self.view viewWithTag:1]; NSString *name = [NSString stringWithFormat:@"第%ld个", segmentControl.numberOfSegments + 1]; [self.titleArr addObject:name]; [segmentControl insertSegmentWithTitle:name atIndex:self.titleArr.count animated:YES]; } #pragma mark 删除 - (void)removeButtonOnClick:(UIButton *)button { UISegmentedControl *segmentControl = (UISegmentedControl *)[self.view viewWithTag:1]; NSString *name = [NSString stringWithFormat:@"第%ld个", self.titleArr.count]; [self.titleArr removeObject:name]; [segmentControl removeSegmentAtIndex:self.titleArr.count animated:YES]; }
相关文章推荐
- SPOJ QTREE4 Query on a tree IV (边分治 + 堆)
- POJ_1986_Distance Queries(LCA+tarjan)
- iOS开发——UI进阶篇(十四)modal
- SPOJ PTO7J Query on a tree III(dfs序+主席树)
- SPOJ QTREE2 Query on a tree II (倍增LCA)
- 想要App做得美,这些UI/UX设计趋势你要知道啊
- SPOJ QTREE Query on a tree (树链剖分)
- UIScrollView上面添加的uiftextfield如何失去第一响应者
- UITableView Cell
- UE4信息交互-蓝图类中的信息交互
- UI 03 关于UITextField键盘遮挡问题
- FZU 1752 Quickmod Quickmulti
- Android UI: LinearLayout中layout_weight 属性的使用规则
- Xcode6以后segue取消原push与modal(deprecated)
- UI 03 UIButton 和 UITextField
- UITextField总结
- Unknown Class XX in Interface Builder File
- UIRectEdge
- UICntrol
- [leetcode-60]Permutation Sequence(C)