如何改变uisegmentedcontrol点击的响应事件
展开全部
NSArray *segmentedArray = [[NSArrayalloc]initWithObjects:@"1",@"2",@"3",@"4",nil];
//初始化UISegmentedControl
UISegmentedControl *segmentedControl = [[UISegmentedControlalloc]initWithItems:segmentedArray];
segmentedControl.frame = CGRectMake(20.0,……)
// 设置默认选择项索引
segmentedControl.selectedSegmentIndex = 2;
segmentedControl.tintColor = [UIColor redColor];
// 有基本四种样式
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;
// 设置在点击后是否恢复原样
segmentedControl.momentary = YES;
[segmentedControl setTitle:@"two" forSegmentAtIndex:1];//设置指定索引的题目
[segmentedControl setImage:[UIImage imageNamed:@"btn_jyy.png"] forSegmentAtIndex:3];//设置指定索引的图片
// 在指定索引插入一个选项并设置图片
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"mei.png"] atIndex:2 animated:NO];
// 在指定索引插入一个选项并设置题目
[segmentedControl insertSegmentWithTitle:@"insert" atIndex:3 animated:NO];
// 移除指定索引的选项
[segmentedControl removeSegmentAtIndex:0 animated:NO];
// 设置指定索引选项的宽度
[segmentedControl setWidth:70.0 forSegmentAtIndex:2];
// 设置选项中图片等的左上角的位置
[segmentedControl setContentOffset:CGSizeMake(10.0,10.0) forSegmentAtIndex:4];
//获取指定索引选项的图片imageForSegmentAtIndex:
UIImageView *imageForSegmentAtIndex = [[UIImageViewalloc]initWithImage:[segmentedControl imageForSegmentAtIndex:1]];
imageForSegmentAtIndex.frame = CGRectMake(60.0, 120.0, 30.0, 30.0); ;
//获取指定索引选项的标题titleForSegmentAtIndex
UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(100.0, 160.0, 30.0, 30.0)];
titleForSegmentAtIndex.text = [segmentedControl titleForSegmentAtIndex:0];
//获取总选项数segmentedControl.numberOfSegments
UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(140.0, 170.0, 30.0, 30.0)];
numberOfSegments.text = [NSString stringWithFormat:@"%d",segmentedControl.numberOfSegments];
//获取指定索引选项的宽度widthForSegmentAtIndex:
UILabel *widthForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(180.0, 210.0, 70.0, 30.0)];
widthForSegmentAtIndex.text = [NSString stringWithFormat:@"%f",[segmentedControl widthForSegmentAtIndex:2]];
// [segmentedControl setEnabled:NO forSegmentAtIndex:4];//设置指定索引选项不可选
// BOOL enableFlag = [segmentedControl isEnabledForSegmentAtIndex:4];//判断指定索引选项是否可选
[mySegmentedControladdTarget:selfaction:@selector(didClicksegmentedControlAction:)forControlEvents:UIControlEventValueChanged];
-(void)didClicksegmentedControlAction:(UISegmentedControl *)Seg{
NSInteger Index = Seg.selectedSegmentIndex;
NSLog(@"Index %i", Index);
switch (Index) {
case 0:
[self selectmyView1];
break;
case 1:
[self selectmyView2];
break;
case 2:
[self selectmyView3];
break;
case 3:
[self selectmyView4];
break;
…………………………………….
default:
break;
}
}
//初始化UISegmentedControl
UISegmentedControl *segmentedControl = [[UISegmentedControlalloc]initWithItems:segmentedArray];
segmentedControl.frame = CGRectMake(20.0,……)
// 设置默认选择项索引
segmentedControl.selectedSegmentIndex = 2;
segmentedControl.tintColor = [UIColor redColor];
// 有基本四种样式
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;
// 设置在点击后是否恢复原样
segmentedControl.momentary = YES;
[segmentedControl setTitle:@"two" forSegmentAtIndex:1];//设置指定索引的题目
[segmentedControl setImage:[UIImage imageNamed:@"btn_jyy.png"] forSegmentAtIndex:3];//设置指定索引的图片
// 在指定索引插入一个选项并设置图片
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"mei.png"] atIndex:2 animated:NO];
// 在指定索引插入一个选项并设置题目
[segmentedControl insertSegmentWithTitle:@"insert" atIndex:3 animated:NO];
// 移除指定索引的选项
[segmentedControl removeSegmentAtIndex:0 animated:NO];
// 设置指定索引选项的宽度
[segmentedControl setWidth:70.0 forSegmentAtIndex:2];
// 设置选项中图片等的左上角的位置
[segmentedControl setContentOffset:CGSizeMake(10.0,10.0) forSegmentAtIndex:4];
//获取指定索引选项的图片imageForSegmentAtIndex:
UIImageView *imageForSegmentAtIndex = [[UIImageViewalloc]initWithImage:[segmentedControl imageForSegmentAtIndex:1]];
imageForSegmentAtIndex.frame = CGRectMake(60.0, 120.0, 30.0, 30.0); ;
//获取指定索引选项的标题titleForSegmentAtIndex
UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(100.0, 160.0, 30.0, 30.0)];
titleForSegmentAtIndex.text = [segmentedControl titleForSegmentAtIndex:0];
//获取总选项数segmentedControl.numberOfSegments
UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(140.0, 170.0, 30.0, 30.0)];
numberOfSegments.text = [NSString stringWithFormat:@"%d",segmentedControl.numberOfSegments];
//获取指定索引选项的宽度widthForSegmentAtIndex:
UILabel *widthForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(180.0, 210.0, 70.0, 30.0)];
widthForSegmentAtIndex.text = [NSString stringWithFormat:@"%f",[segmentedControl widthForSegmentAtIndex:2]];
// [segmentedControl setEnabled:NO forSegmentAtIndex:4];//设置指定索引选项不可选
// BOOL enableFlag = [segmentedControl isEnabledForSegmentAtIndex:4];//判断指定索引选项是否可选
[mySegmentedControladdTarget:selfaction:@selector(didClicksegmentedControlAction:)forControlEvents:UIControlEventValueChanged];
-(void)didClicksegmentedControlAction:(UISegmentedControl *)Seg{
NSInteger Index = Seg.selectedSegmentIndex;
NSLog(@"Index %i", Index);
switch (Index) {
case 0:
[self selectmyView1];
break;
case 1:
[self selectmyView2];
break;
case 2:
[self selectmyView3];
break;
case 3:
[self selectmyView4];
break;
…………………………………….
default:
break;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐于2016-06-09 · 知道合伙人数码行家
可以叫我表哥
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:25897
获赞数:1464984
2010年毕业于北京化工大学北方学院计算机科学与技术专业毕业,学士学位,工程电子技术行业4年从业经验。
向TA提问 私信TA
关注
展开全部
在做ios开发时,经常使用到UISegmentedControl控件,而我们也经常需要去改变UISegmentedControl的颜色,而总是发现无法改变,其实并非无法改变,而是UISegmentedControl的样式选择错了。
你会发现有时候能够改变颜色而有时候却改变了也没有效果。
仔细看一下api函数,你会发现真正的原因:
/* Default tintColor is nil. Only used if style is UISegmentedControlStyleBar or UISegmentedControlStyleBezeled
看到上面这句话,你可能已经明白了,为什么你设置的颜色有时候可以出现,而有时候却不能出现,这其中的原因就跟样式有关。只要你选择对了样式,都可以对u i se gment的颜色进行修改。
segmentedControl.segmentedControlStyle= UISegmentedControlStyleBar;//设置
segmentedControl.tintColor= [UIColorcolorWithRed:0.23green:0.50blue:0.82alpha:0.90];
你会发现有时候能够改变颜色而有时候却改变了也没有效果。
仔细看一下api函数,你会发现真正的原因:
/* Default tintColor is nil. Only used if style is UISegmentedControlStyleBar or UISegmentedControlStyleBezeled
看到上面这句话,你可能已经明白了,为什么你设置的颜色有时候可以出现,而有时候却不能出现,这其中的原因就跟样式有关。只要你选择对了样式,都可以对u i se gment的颜色进行修改。
segmentedControl.segmentedControlStyle= UISegmentedControlStyleBar;//设置
segmentedControl.tintColor= [UIColorcolorWithRed:0.23green:0.50blue:0.82alpha:0.90];
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询