UISegmentedControl的詳細(xì)使用

當(dāng)用戶輸入不僅僅是布爾值時,可使用分段控件 (UISegmentedControl)。分段控件提供一欄按鈕(有時稱為按鈕欄),但只能激活其中一個按鈕。分段控件會導(dǎo)致用戶在屏幕上看到的內(nèi)容發(fā) 生變化。它們常用于在不同類別的信息之間選擇,或在不同的應(yīng)用屏幕之間切換。下面介紹基本屬性和基本方法的使用。

1.初始化UISegmentedControl

初始化方法:傳入的數(shù)組可以是字符串也可以是UIImage對象的圖片數(shù)組


NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",nil];

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];

segmentedControl.frame = CGRectMake(20.0, 20.0, 250.0, 50.0);

segmentedControl.selectedSegmentIndex = 2;//設(shè)置默認(rèn)選擇項索引

segmentedControl.tintColor = [UIColor redColor];

2. 設(shè)置樣式

??:這個屬性已經(jīng)廢棄,不再起任何作用,它的枚舉如下:

segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain



3.設(shè)置在點擊后是否恢復(fù)原樣

segmentedControl.momentary = YES

4.其他屬性


設(shè)置標(biāo)簽寬度是否隨內(nèi)容自適應(yīng):

@property(nonatomic) BOOL apportionsSegmentWidthsByContent;

注意:如果設(shè)置為NO,則所有標(biāo)簽寬度一致,為最大寬度。

5.獲取指定索引選項的圖片imageForSegmentAtIndex:

UIImageView *imageForSegmentAtIndex = [[UIImageViewalloc]initWithImage:[segmentedControl imageForSegmentAtIndex:1]];

imageForSegmentAtIndex.frame = CGRectMake(60.0, 120.0, 30.0, 30.0);? ;

6.獲取指定索引選項的標(biāo)題titleForSegmentAtIndex

UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(100.0, 160.0, 30.0, 30.0)];

titleForSegmentAtIndex.text = [segmentedControl titleForSegmentAtIndex:0];

7.獲取總選項數(shù)segmentedControl.numberOfSegments

UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(140.0, 170.0, 30.0, 30.0)];

numberOfSegments.text = [NSString stringWithFormat:@"%d",segmentedControl.numberOfSegments];

8.獲取指定索引選項的寬度widthForSegmentAtIndex:

UILabel *widthForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(180.0, 210.0, 70.0, 30.0)];

widthForSegmentAtIndex.text = [NSString stringWithFormat:@"%f",[segmentedControl widthForSegmentAtIndex:2]];

9.設(shè)置指定索引選項不可選

// [segmentedControl setEnabled:NO forSegmentAtIndex:4];

10.判斷指定索引選項是否可選

// BOOL enableFlag = [segmentedControl isEnabledForSegmentAtIndex:4];

11.添加委托方法

[mySegmentedControladdTarget:selfaction:@selector(segmentAction:)forControlEvents:UIControlEventValueChanged]; ?

12.實現(xiàn)委托方法

-(void)segmentAction:(UISegmentedControl *)Seg{

NSInteger Index = Seg.selectedSegmentIndex;

NSLog(@"Index %i", Index);

switch (Index) {

case 0:

[self selectmyView1];

break;

case 1:

[self selectmyView2];

break;

case 2:

[self selectmyView3];

break;

case 3:

[self selectmyView4];

break;

case 4:

[self selectmyView5];

break;

case 5:

[self selectmyView6];

break;

default:

break;

}

}

13. 項目中實際應(yīng)用,放在導(dǎo)航欄中

自定義UISegmentedcontrol

UISegmentedControl *segmentedControl=[[UISegmentedControl alloc] initWithFrame:CGRectMake(80.0f, 8.0f, 200.0f, 30.0f) ];

[segmentedControl insertSegmentWithTitle:@"1" atIndex:0 animated:YES];

[segmentedControl insertSegmentWithTitle:@"2" atIndex:1 animated:YES];

segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;

segmentedControl.momentary = YES;

segmentedControl.multipleTouchEnabled=NO;

[segmentedControl addTarget:self action:@selector(Selectbutton:) forControlEvents:UIControlEventValueChanged];

UIBarButtonItem *segButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];? //自定義UIBarButtonItem,封裝定義好的UIsegmented。

[segmentedControl release];

self.navigationItem.rightBarButtonItem = segButton;? //添加到導(dǎo)航欄中

[segButton release];


14.??:下面是一些找到的圖片,在xib中的一些設(shè)置


主要參考分段選擇器

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • iOS開發(fā)系列--網(wǎng)絡(luò)開發(fā) 概覽 大部分應(yīng)用程序都或多或少會牽扯到網(wǎng)絡(luò)開發(fā),例如說新浪微博、微信等,這些應(yīng)用本身可...
    lichengjin閱讀 4,055評論 2 7
  • //設(shè)置尺寸為屏幕尺寸的時候self.window = [[UIWindow alloc] initWithFra...
    LuckTime閱讀 980評論 0 0
  • 1、設(shè)置UILabel行間距 NSMutableAttributedString* attrString = [[...
    十年一品溫如言1008閱讀 2,048評論 0 3
  • 七月流火,八月未央。 八月的第一天,烈日與暴雨次第登場,塵埃飛揚(yáng)。 這樣的時光,總是最長又最短,像那些被我們揮霍后...
    自由落體的瓦閱讀 508評論 3 5
  • 姓名:庹亞軍 公司:寧波貞觀電器有限公司 組別:第235期 利他一組 【日精進(jìn)打卡第 53天】 【知~學(xué)習(xí)】 《六...
    tyj小電工閱讀 122評論 0 0

友情鏈接更多精彩內(nèi)容