IOS學(xué)習(xí)筆記(七)之UISegmentedControl分段控件的基本概念和使用方法
成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的三河網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
Author:hmjiangqq
Email:jiangqqlmj@163.com
UISegmentedControl
首先看下官網(wǎng)介紹:
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.
分段控件是一種選擇控件,功能有點(diǎn)類似于Windows中的單選按鈕,由兩端或者更多段組成,
每個(gè)段相當(dāng)于一個(gè)獨(dú)立的按鈕。這控件一般有兩種樣式-Plain&Bordered樣式和Bar樣式,
Bordered樣式是在Plain樣式上面加上了一個(gè)邊框.Plain和Bordered樣式中每一段都可以設(shè)置文本
和添加圖片.Bar樣式整體來(lái)說(shuō)比較窄,在其中我們一般不會(huì)放置圖片。
常用的屬性和方法如下:
實(shí)例代碼如下:
- (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; //設(shè)置位置 大小 segmentControl.frame=CGRectMake(60, 100, 200, 40); //默認(rèn)選擇 segmentControl.selectedSegmentIndex=1; //設(shè)置背景色 segmentControl.tintColor=[UIColor greenColor]; //設(shè)置監(jiān)聽(tīng)事件 [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); }
運(yùn)行截圖: