iOS开发基础控件--UISegmentedControl

         

         分段控件是我们常用的控件之一,今天把具体用法总结了下:

          1.初始化UISegmentedControl

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",nil];  
  2. UISegmentedControl *segmentedTemp = [[UISegmentedControl alloc]initWithItems:segmentedArray];  
  3. self.segmentedControl = segmentedTemp;  
  4. segmentedControl.frame = CGRectMake(10.0, 10.0, 300.0, 29.0);  
  5.   
  6.      2.常用属性及设置方法如下:  
  7. //设置指定索引的题目  
  8. [segmentedControl setTitle:@"1" forSegmentAtIndex:1];  
  9. //设置指定索引的图片  
  10. [segmentedControl setImage:[UIImage imageNamed:@"home.png"] forSegmentAtIndex:2];  
  11. //在指定索引插入一个选项并设置图片  
  12. [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"more.png"] atIndex:2 animated:NO];  
  13. //在指定索引插入一个选项并设置题目  
  14. [segmentedControl insertSegmentWithTitle:@"new" atIndex:3 animated:NO];  
  15. //移除指定索引的选项  
  16. [segmentedControl removeSegmentAtIndex:0 animated:NO];  
  17. //设置指定索引选项的宽度  
  18. [segmentedControl setWidth:60.0 forSegmentAtIndex:2];  
  19. //设置选项中图片等的左上角的位置  
  20. //[segmentedControl setContentOffset:CGSizeMake(10.0,10.0) forSegmentAtIndex:1];  
  21.   
  22. //设置默认选择项索引  
  23. segmentedControl.selectedSegmentIndex = 2;  
  24. //分段控件的颜色,只有样式为UISegmentedControlStyleBar的时候才有效果  
  25. segmentedControl.tintColor = [UIColor redColor];  
  26. //设置样式  
  27. segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;  
  28. //设置在点击后是否恢复原样  
  29. segmentedControl.momentary = NO;  
  30. //设置指定索引选项不可选  
  31. [segmentedControl setEnabled:NO forSegmentAtIndex:3];  
  32. //判断指定索引选项是否可选  
  33. BOOL enableFlag = [segmentedControl isEnabledForSegmentAtIndex:3];  
  34. NSLog(@"%d",enableFlag);  

        3.分段控件点击事件:

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [segmentedControl addTarget:self  
  2.                      action:@selector(segmentAction:)  
  3.            forControlEvents:UIControlEventValueChanged];  

        响应的事件:

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. -(void)segmentAction:(UISegmentedControl *)Seg  
  2. {  
  3.     NSInteger index = Seg.selectedSegmentIndex;  
  4.     switch (index) {  
  5.         case 0:  
  6.             NSLog(@"0 clicked.");  
  7.             break;  
  8.         case 1:  
  9.             NSLog(@"1 clicked.");  
  10.             break;  
  11.         case 2:  
  12.             NSLog(@"2 clicked.");  
  13.             break;  
  14.         case 3:  
  15.             NSLog(@"3 clicked.");  
  16.             break;  
  17.         case 4:  
  18.             NSLog(@"4 clicked.");  
  19.             break;  
  20.         default:  
  21.             break;  
  22.     }  
  23. }  

         4.获取分段控件相应的值:

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //获取指定索引选项的图片imageForSegmentAtIndex:  
  2. UIImageView *imageForSegmentAtIndex = [[UIImageView alloc]initWithImage:[segmentedControl imageForSegmentAtIndex:1]];  
  3. imageForSegmentAtIndex.frame = CGRectMake(60.0, 100.0, 30.0, 30.0);  
  4.   
  5. //获取指定索引选项的标题titleForSegmentAtIndex  
  6. UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(100.0, 100.0, 30.0, 30.0)];  
  7. titleForSegmentAtIndex.text = [segmentedControl titleForSegmentAtIndex:0];  
  8.   
  9. //获取总选项数segmentedControl.numberOfSegments  
  10. UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(140.0, 100.0, 30.0, 30.0)];  
  11. numberOfSegments.text = [NSString stringWithFormat:@"%d",segmentedControl.numberOfSegments];  
  12.   
  13. //获取指定索引选项的宽度widthForSegmentAtIndex:  
  14. UILabel *widthForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(180.0, 100.0, 70.0, 30.0)];  
  15. widthForSegmentAtIndex.text = [NSString stringWithFormat:@"%f",[segmentedControl widthForSegmentAtIndex:2]];  

         但是这样的分段控件只有固定的几种样式。在IOS5以后,可以全局的设置一些控件的外观,分段控件就是其中一个(全局设置UISegmentedControl外观):

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //cap insets用来指定哪些区域是固定不变的,未制定的区域则会repeat    
  2.     
  3. UIImage *segmentSelected = [[UIImage imageNamed:@"bg_o.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];    
  4.     
  5. UIImage *segmentUnselected = [[UIImage imageNamed:@"bg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];    
  6.     
  7. UIImage *segmentSelectedUnselected = [UIImage imageNamed:@"line.png"] ;    
  8.     
  9. UIImage *segUnselectedSelected = [UIImage imageNamed:@"line.png"] ;    
  10.     
  11. UIImage *segmentUnselectedUnselected = [UIImage imageNamed:@"line.png"];    
  12.     
  13. //Segmente未选中背景    
  14. [[UISegmentedControl appearance] setBackgroundImage:segmentUnselected    
  15.                                            forState:UIControlStateNormal    
  16.                                          barMetrics:UIBarMetricsDefault];    
  17.     
  18. //Segmente选中背景    
  19. [[UISegmentedControl appearance] setBackgroundImage:segmentSelected    
  20.                                            forState:UIControlStateSelected    
  21.                                          barMetrics:UIBarMetricsDefault];    
  22.     
  23. //Segmente左右都未选中时的分割线    
  24. //BarMetrics表示navigation bar的状态,UIBarMetricsDefault 表示portrait状态(44pixel height),UIBarMetricsLandscapePhone 表示landscape状态(32pixel height)    
  25.     
  26. [[UISegmentedControl appearance] setDividerImage:segmentUnselectedUnselected    
  27.                              forLeftSegmentState:UIControlStateNormal    
  28.                                rightSegmentState:UIControlStateNormal    
  29.                                       barMetrics:UIBarMetricsDefault];    
  30.     
  31. [[UISegmentedControl appearance] setDividerImage:segmentSelectedUnselected    
  32.                              forLeftSegmentState:UIControlStateSelected    
  33.                                rightSegmentState:UIControlStateNormal    
  34.                                       barMetrics:UIBarMetricsDefault];    
  35.     
  36. [[UISegmentedControl appearance] setDividerImage:segUnselectedSelected    
  37.                              forLeftSegmentState:UIControlStateNormal    
  38.                                rightSegmentState:UIControlStateSelected    
  39.                                       barMetrics:UIBarMetricsDefault];    
  40.   
  41. //字体    
  42. NSDictionary *textAttibutesUnSelected = [NSDictionary dictionaryWithObjectsAndKeys:    
  43.                                [UIFont systemFontOfSize:18],UITextAttributeFont,    
  44.                                [UIColor blackColor],UITextAttributeTextColor,    
  45.                                [UIColor whiteColor],UITextAttributeTextShadowColor,    
  46.                                [NSValue valueWithCGSize:CGSizeMake(1, 1)],UITextAttributeTextShadowOffset,nil];    
  47.     
  48. NSDictionary *textAttibutesSelected = [NSDictionary dictionaryWithObjectsAndKeys:    
  49.                                          [UIFont systemFontOfSize:18],UITextAttributeFont,    
  50.                                          [UIColor whiteColor],UITextAttributeTextColor,    
  51.                                          [UIColor whiteColor],UITextAttributeTextShadowColor,    
  52.                                          [NSValue valueWithCGSize:CGSizeMake(0, 0)],UITextAttributeTextShadowOffset,nil];    
  53.     
  54. [[UISegmentedControl appearance] setTitleTextAttributes:textAttibutesUnSelected    
  55.                                                forState:UIControlStateNormal];    
  56.     
  57. [[UISegmentedControl appearance] setTitleTextAttributes:textAttibutesSelected    
  58.                                                forState:UIControlStateSelected];   

转载于:https://www.cnblogs.com/songfeixiang/p/3733729.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值