UISegmentedControl分段控件的基本概念和使用方法

转自:博客地址:http://blog.csdn.net/developer_jiangqq)

Author:hmjiangqq

Email:jiangqqlmj@163.com

UISegmentedControl

首先看下官网介绍:


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 image (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样式整体来说比较窄,在其中我们一般不会放置图片。

常用的属性和方法如下:


实例代码如下:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  2. {  
  3.     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];  
  4.     // Override point for customization after application launch.  
  5.     self.window.backgroundColor = [UIColor whiteColor];  
  6.       
  7.     NSArray *array=@[@"搜索",@"选择",@"视频",@"图片"];  
  8.     UISegmentedControl *segmentControl=[[UISegmentedControl alloc]initWithItems:array];  
  9.     segmentControl.segmentedControlStyle=UISegmentedControlStyleBordered;  
  10.     //设置位置 大小  
  11.     segmentControl.frame=CGRectMake(6010020040);  
  12.     //默认选择  
  13.     segmentControl.selectedSegmentIndex=1;  
  14.     //设置背景色  
  15.     segmentControl.tintColor=[UIColor greenColor];  
  16.     //设置监听事件  
  17.     [segmentControl addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];  
  18.     [self.window addSubview:segmentControl];  
  19.     [segmentControl release];  
  20.       
  21.     [self.window makeKeyAndVisible];  
  22.     return YES;  
  23. }  
  24.   
  25. -(void)change:(UISegmentedControl *)segmentControl{  
  26.     NSLog(@"segmentControl %d",segmentControl.selectedSegmentIndex);  
  27. }  
运行截图:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值