iOS App開發(fā)中的UISegmentedControl分段組件用法總結(jié)
UISegmentedControl分段控件代替了桌面OS上的單選按鈕。不過它的選項(xiàng)個(gè)數(shù)非常有限,因?yàn)槟愕腎OS設(shè)備屏幕有限。當(dāng)我們需要使用選項(xiàng)非常少的單選按鈕時(shí)它很合適。
一、創(chuàng)建
UISegmentedControl* mySegmentedControl = [[UISegmentedControl alloc]initWithItems:nil];
是不是很奇怪沒有指定位置和大小呢?沒錯(cuò),我確實(shí)在他的類聲明里只找到 initWithItems 而未找到 initWithFrame ,所以他不需要指定,不過我看到了另一個(gè)方法,這個(gè)方法可以設(shè)置Item的寬度:
mySegmentedControl setWidth:100 forSegmentAtIndex:0];//設(shè)置Item的寬度
二、屬性
mySegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;//風(fēng)格
可以視使用的場(chǎng)合,有三種風(fēng)格選擇,如下:
typedef enum {
UISegmentedControlStylePlain, // large plain 有灰邊的大白按鈕,適合偏好設(shè)置單元
UISegmentedControlStyleBordered, // large bordered 黑邊的大白按鈕,適用于表格單元
UISegmentedControlStyleBar, // small button/nav bar style. tintable 小按鈕,適合導(dǎo)航欄
UISegmentedControlStyleBezeled, // large bezeled style. tintable
} UISegmentedControlStyle;
如果你使用的是 UISegmentedControlStyleBar 風(fēng)格,還可以用空間的 tintColor 屬性為整個(gè)控件設(shè)置渲染色彩:
UIColor *myTint = [[ UIColor alloc]initWithRed:0.66 green:1.0 blue:0.77 alpha:1.0];
mySegmentedControl.tintColor = myTint;
三、添加、刪除片段
每個(gè)分段控件的片段都是一個(gè)按鈕,其中包含一個(gè)標(biāo)簽或圖片。你需要在你的控件中為每個(gè)控件創(chuàng)建一個(gè)片段。只要屏幕放得下,就可以有許多片段,但用戶同一時(shí)刻只能選擇一個(gè)片段。
[mySegmentedControl insertSegmentWithTitle:@"First" atIndex:0 animated:YES];
[mySegmentedControl insertSegmentWithTitle:@"Second" atIndex:2 animated:YES];
每個(gè)
按鈕都被賦予一個(gè)索引,用這個(gè)索排序以及標(biāo)識(shí)。
你也可以添加一個(gè)含有圖像的片段,用inserSegmentWithImage
[mySegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"pic"] atIndex:3 animated:YES];
刪除片段
[mySegmentedControl removeSegmentAtIndex:0 animated:YES];//刪除一個(gè)片段
[mySegmentedControl removeAllSegments];//刪除所有片段
四、片段標(biāo)題
[mySegmentedControl setTitle:@"ZERO" forSegmentAtIndex:0];//設(shè)置標(biāo)題
NSString* myTitle = [mySegmentedControl titleForSegmentAtIndex:1];//讀取標(biāo)題
五、圖像
每個(gè)分段也可以設(shè)置圖像:
[mySegmentedControl setImage:[UIImage imageNamed:@"pic"] forSegmentAtIndex:1];//設(shè)置
UIImage* myImage = [mySegmentedControl imageForSegmentAtIndex:2];//讀取
注意:圖像不會(huì)自動(dòng)調(diào)整大小,圖片多大就會(huì)原生地顯示多大,所以你要通知做圖的美工大小要精確。
六、選中分段
分段控件的默認(rèn)行為是,一旦按鈕被選中就一直保持,直到另外一個(gè)按鈕被選中為止。你可以改變這種默認(rèn)的行為,變成按鈕按下后很快就自動(dòng)釋放。將控件的momentary屬性設(shè)為YES:
mySegmentedControl.momentary = YES;
注意:開啟這個(gè)功能后點(diǎn)觸片段不會(huì)更新 selectedSegmentedIndex,因此也就無法通過這個(gè)屬性得到當(dāng)前選取的片段。
初始化默認(rèn)片段
默認(rèn)情況下,除非你指定,否則不會(huì)有任何片段被選中。要設(shè)置 selectedSegmentedIndex 屬性:
mySegmentedControl.selectedSegmentedIndex = 0;
七、顯示控件
[parentView addSubview:mySegmentedControl];//添加到父視圖
或
self.navigationItem.titleView = mySegmentedControl;//添加到導(dǎo)航欄
八、讀取控件
通過 selectedSegmentedIndex 屬性,可以讀取當(dāng)前選中分段的值,這個(gè)值就是選中片段的索引號(hào)。
int x = mySegmentedControl. selectedSegmentedIndex;
九、通知
要接收片段選取的通知,可以用UIControl類的 addTarget 方法,為 UIControlEventValueChanged 事件添加一個(gè)動(dòng)作:
[mySegmentedControl addTarget:self action:@selector(selected:) forControlEvents:UIControlEventValueChanged];
只要選中了一個(gè)片段,你的動(dòng)作方法就會(huì)被調(diào)用:
-(void)selected:(id)sender{
UISegmentedControl* control = (UISegmentedControl*)sender;
switch (control.selectedSegmentIndex) {
case 0:
//
break;
case 1:
//
break;
case 2:
//
break;
default:
break;
}
}
十、設(shè)置圓角以及設(shè)置選中顏色為空
UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"設(shè)置",@"知道了", nil]];
seg.frame = CGRectMake(10,0, CGRectGetWidth(self.view.frame) - 20, 35);
seg.center = isbluetoothOffAlerView.center;
seg.layer.borderColor = [UIColor whiteColor].CGColor;
seg.layer.borderWidth = 2;
seg.tintColor = [UIColor whiteColor];
seg.backgroundColor = [UIColor clearColor];
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,[UIFont systemFontOfSize:24],NSFontAttributeName,nil];
[seg setTitleTextAttributes:dic forState:UIControlStateNormal];
seg.layer.cornerRadius = 15;
seg.layer.masksToBounds = YES;
相關(guān)文章
實(shí)例解析iOS app開發(fā)中音頻文件播放工具類的封裝
這篇文章主要介紹了iOS app開發(fā)中音頻文件播放工具類的封裝,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01詳解iOS WebDriverAgent 環(huán)境搭建
這篇文章主要介紹了詳解iOS WebDriverAgent 環(huán)境搭建,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01iOS利用AFNetworking3.0——實(shí)現(xiàn)文件斷點(diǎn)下載
這篇文章主要介紹了iOS利用AFNetworking3.0——實(shí)現(xiàn)文件斷點(diǎn)下載,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01iOS開發(fā)中常見的解析XML的類庫(kù)以及簡(jiǎn)要安裝方法
這篇文章主要介紹了iOS開發(fā)中常見的解析XML的類庫(kù)以及簡(jiǎn)要安裝方法,簡(jiǎn)要地說明了在Xcode下的一些特點(diǎn)以供對(duì)比,需要的朋友可以參考下2015-10-10iOS使用fastlane實(shí)現(xiàn)持續(xù)集成的方法教程
這篇文章主要給大家介紹了關(guān)于iOS使用fastlane如何實(shí)現(xiàn)持續(xù)集成的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

iOS Swift創(chuàng)建代理協(xié)議的多種方式示例