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

1.初始化UISegmentedControl

[plain] view
plain
copy

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

3.分段控件点击事件:

[plain] view
plain
copy

  1. [segmentedControl addTarget:self
  2. action:@selector(segmentAction:)
  3. forControlEvents:UIControlEventValueChanged];

响应的事件:

[plain] view
plain
copy

  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

  1. //获取指定索引选项的图片imageForSegmentAtIndex:
  2. UIImageView *imageForSegmentAtIndex = [[UIImageView alloc]initWithImage:[segmentedControl imageForSegmentAtIndex:1]];
  3. imageForSegmentAtIndex.frame = CGRectMake(60.0, 100.0, 30.0, 30.0);
  4. //获取指定索引选项的标题titleForSegmentAtIndex
  5. UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(100.0, 100.0, 30.0, 30.0)];
  6. titleForSegmentAtIndex.text = [segmentedControl titleForSegmentAtIndex:0];
  7. //获取总选项数segmentedControl.numberOfSegments
  8. UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(140.0, 100.0, 30.0, 30.0)];
  9. numberOfSegments.text = [NSString stringWithFormat:@"%d",segmentedControl.numberOfSegments];
  10. //获取指定索引选项的宽度widthForSegmentAtIndex:
  11. UILabel *widthForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(180.0, 100.0, 70.0, 30.0)];
  12. widthForSegmentAtIndex.text = [NSString stringWithFormat:@"%f",[segmentedControl widthForSegmentAtIndex:2]];

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

[plain] view
plain
copy

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

iOS开发基础控件--UISegmentedControl的更多相关文章

  1. iOS开发基础控件--UIButton

    01 //这里创建一个圆角矩形的按钮 02     UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 03 ...

  2. iOS开发基础控件--UILabel

    UILabel 的常见属性和方法: //创建UIlabel对象 UILabel* label = [[UILabel alloc] initWithFrame:self.view.bounds]; / ...

  3. iOS开发基础控件--UITextField

    001 //初始化textfield并设置位置及大小 002   UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20 ...

  4. iOS:分段控件UISegmentedControl的详细使用

    分段控件:UISegmentedControl   功能:分段的控制.页面的切换等.   介绍:当用户输入不仅仅是布尔值时,可使用分段控件(UISegmentedControl).分段控件提供一栏按钮 ...

  5. IOS(一) 基础控件的介绍以及使用

    IOS的界面的制作,相对于Android来说 简洁了很多,虽然创建布局的方式都是两种(代码创建.布局文件) 但是Android中的xml布局文件在某些方面也属于代码创建,因为自己使用到得每一个属性 都 ...

  6. 【ios开发】控件细究1:UITableView

    工作了将近两个月,共接手两个项目,在项目中用的最多的就是UITableView了,但是也是问题出现的最多的地方,由于一开始不熟练,导致很多问题花了很长时间才解决.所以利用这两天空闲时间,好好梳理一下这 ...

  7. iOS开发-DatePicker控件

    时间控件不管是Android还是iOS中都是必然存在的一个控件,具体的效果大同小异,显示日期,时间,iOS中有四种方式可以选择,Time, Date,Date and Time  , Count Do ...

  8. IOS开发之控件篇UINavigationController第一章 - 介绍

    UINavigationController是一个比较常见的控件,它连接个视图,例如一个视图走到另外一个视图,之间的联系都可以用这个NavigationController的方法 一般都会由两个部分组 ...

  9. IOS开发之控件篇UICollectionViewControllor第一章 - 普通介绍

    1.介绍 UICollectionView和UICollectionViewControllor是IOS6.0后引入的新控件 使用UICollectionView必须实现三个接口: UICollect ...

随机推荐

  1. 一个简洁、好用的Pytorch训练模板

    一个简洁.好用的Pytorch训练模板 代码地址:https://github.com/KinglittleQ/Pytorch-Template 怎么使用 1) 更改template.py 替换 __ ...

  2. Redis 字符串与哈希

    /*** * 字符串 redis里的字符串 ***/ //设置key的值 redis 127.0.0.1:6379> set key 'my name is imay' //设置值的过期时间 ( ...

  3. 《DSP using MATLAB》示例Example 8.5

  4. caddy quic 协议试用&& 几个问题

    备注:    caddy  具体的安装就不介绍,quic 协议也不介绍了   1. 启用协议,比较简单 /usr/local/bin/caddy -log stdout -quic -conf=/et ...

  5. 洛谷 4525 && 洛谷 4526 【模板】自适应辛普森法

    题目:https://www.luogu.org/problemnew/show/P4525 https://www.luogu.org/problemnew/show/P4526 参考:https: ...

  6. mysql binlog_format row and Statement 比较

    两种模式的对比: Statement 优点 历史悠久,技术成熟: 产生的 binlog 文件较小: binlog 中包含了所有数据库修改信息,可以据此来审核数据库的安全等情况: binlog 可以用于 ...

  7. ruby关于require路径

    ruby里面的require说明 require './aaaa' 这种方式,包含的是系统路径 相对路径得用下面的 require_relative "./xxxx" 或者使用这个 ...

  8. "废物利用"也抄袭——废旧喷墨打印机和光驱DIY"绘图仪"

    很长时间没有写博客,因为各种各样的事情占去大块时间,只有零碎时间偶尔在CSDN逛逛也偶尔回几个帖子.很久以前就看到一些光驱DIY雕刻机之类的,很是向往,最近这几天得闲就TB了一套Arduino UNO ...

  9. 【转】Android开发笔记——圆角和边框们

    原文地址:http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/ Android开发笔记——圆角和边框们 在做Androi ...

  10. dubbo 梗概及使用示例

    阿里巴巴dubbo主页:http://code.alibabatech.com/wiki/display/dubbo/Home-zh 1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提 ...