十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
IOS学习笔记(七)之UISegmentedControl分段控件的基本概念和使用方法
成都创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站建设、成都网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的三河网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
Author:hmjiangqq
Email:jiangqqlmj@163.com
UISegmentedControl
首先看下官网介绍:
A UISegmentedControl
object is a horizontal control made of multiple segments, each segment functioning as a discrete button. A segmented control affords a compact means to group together a number of controls.
A segmented control can display a title (an NSString
object) or an p_w_picpath (UIImage
object). The UISegmentedControl
object automatically resizes segments to fit proportionally within their superview unless they have a specific width set. When you add and remove segments, you can request that the action be animated with sliding and fading effects.
分段控件是一种选择控件,功能有点类似于Windows中的单选按钮,由两端或者更多段组成,
每个段相当于一个独立的按钮。这控件一般有两种样式-Plain&Bordered样式和Bar样式,
Bordered样式是在Plain样式上面加上了一个边框.Plain和Bordered样式中每一段都可以设置文本
和添加图片.Bar样式整体来说比较窄,在其中我们一般不会放置图片。
常用的属性和方法如下:
实例代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; NSArray *array=@[@"搜索",@"选择",@"视频",@"图片"]; UISegmentedControl *segmentControl=[[UISegmentedControl alloc]initWithItems:array]; segmentControl.segmentedControlStyle=UISegmentedControlStyleBordered; //设置位置 大小 segmentControl.frame=CGRectMake(60, 100, 200, 40); //默认选择 segmentControl.selectedSegmentIndex=1; //设置背景色 segmentControl.tintColor=[UIColor greenColor]; //设置监听事件 [segmentControl addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged]; [self.window addSubview:segmentControl]; [segmentControl release]; [self.window makeKeyAndVisible]; return YES; } -(void)change:(UISegmentedControl *)segmentControl{ NSLog(@"segmentControl %d",segmentControl.selectedSegmentIndex); }
运行截图: