SegmentedControl又被稱作分段控制器,是IOS開發(fā)中經(jīng)常用到的一個(gè)UI控件。
1通常是在單視圖中使用,不做多視圖之間的切換。實(shí)現(xiàn)視圖中不同顯示的快速切換,每一個(gè)分割表示一個(gè)不同的顯示,這些顯示往往是相關(guān)的,所謂的相關(guān),可以理解成,功能一樣,但是屬性類別有差異。
比較常用的還有比如說,在一個(gè)視圖中,不同的分割控制tableView加載不同的數(shù)據(jù)源。
2 它通常在整個(gè)屏幕的上部,不是必然,但大部分情況下是這樣用。
3 一般是3到5個(gè)分割,超過5個(gè)的話每個(gè)分割的大小對(duì)于用戶觸碰的體驗(yàn)會(huì)很差。
4 任意時(shí)刻,只有一個(gè)分割是激活狀態(tài)的。有點(diǎn)像單選按鈕。
-?(void)initSegmentedControl
{
NSArray*segmentedData?=?[[NSArrayalloc]initWithObjects:@"apple",@"orange",@"banana",nil];
UISegmentedControl*segmentedControl?=?[[UISegmentedControlalloc]initWithItems:segmentedData];
segmentedControl.frame=?CGRectMake(10.0,20.0,300.0,30.0);
/*
這個(gè)是設(shè)置按下按鈕時(shí)的顏色
*/
segmentedControl.tintColor=?[UIColorcolorWithRed:49.0/256.0green:148.0/256.0blue:208.0/256.0alpha:1];
segmentedControl.selectedSegmentIndex=0;//默認(rèn)選中的按鈕索引
/*
下面的代碼實(shí)同正常狀態(tài)和按下狀態(tài)的屬性控制,比如字體的大小和顏色等
*/
NSDictionary*attributes?=?[NSDictionarydictionaryWithObjectsAndKeys:[UIFontboldSystemFontOfSize:12],NSFontAttributeName,[UIColorredColor],?NSForegroundColorAttributeName,nilnil];
[segmentedControlsetTitleTextAttributes:attributesforState:UIControlStateNormal];
NSDictionary*highlightedAttributes?=?[NSDictionarydictionaryWithObject:[UIColorredColor]forKey:NSForegroundColorAttributeName];
[segmentedControlsetTitleTextAttributes:highlightedAttributesforState:UIControlStateHighlighted];
//設(shè)置分段控件點(diǎn)擊相應(yīng)事件
[segmentedControladdTarget:selfaction:@selector(doSomethingInSegment:)forControlEvents:UIControlEventValueChanged];
[self.viewaddSubview:segmentedControl];
}
每個(gè)功能注釋都有清晰的描述,有一點(diǎn)要特別說明一下:
在ios7以前,segmentedcontrol有一個(gè)segmentedControlStyle 屬性,通常都要設(shè)置,比如像下面這樣:
/*
typedef?enum?{
UISegmentedControlStylePlain,
UISegmentedControlStyleBordered,
UISegmentedControlStyleBar,
UISegmentedControlStyleBezeled,
}?UISegmentedControlStyle;
*/
segmentedControl.segmentedControlStyle=?UISegmentedControlStyleBar;
但是這個(gè)在ios7之后,出于扁平化風(fēng)格的考慮,這些style都不在有效了
我們?cè)賹懸粋€(gè)按鈕的事件響應(yīng)函數(shù),設(shè)置不同的背景圖片,如下:
-(void)doSomethingInSegment:(UISegmentedControl*)Seg
{
NSInteger?Index?=?Seg.selectedSegmentIndex;
switch(Index)
{
case0:
self.view.backgroundColor=?[UIColorcolorWithPatternImage:[UIImageimageNamed:kSrcName(@"bg_apple_small.png")]];
break;
case1:
self.view.backgroundColor=?[UIColorcolorWithPatternImage:[UIImageimageNamed:kSrcName(@"bg_orange_small.png")]];
break;
case2:
self.view.backgroundColor=?[UIColorcolorWithPatternImage:[UIImageimageNamed:kSrcName(@"bg_banana_small.png")]];
break;
default:
break;
}
}
設(shè)置是否保持選中狀態(tài):
@property(nonatomic,getter=isMomentary) BOOL momentary;
注意:如果設(shè)置為YES,點(diǎn)擊結(jié)束后,將不保持選中狀態(tài),默認(rèn)為NO
獲取標(biāo)簽個(gè)數(shù):(只讀)
@property(nonatomic,readonly) NSUInteger numberOfSegments;
設(shè)置標(biāo)簽寬度是否隨內(nèi)容自適應(yīng):
@property(nonatomic) BOOL apportionsSegmentWidthsByContent;
注意:如果設(shè)置為NO,則所有標(biāo)簽寬度一致,為最大寬度。
插入文字標(biāo)簽在index位置:
- (void)insertSegmentWithTitle:(NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated
插入圖片標(biāo)簽在index位置
- (void)insertSegmentWithImage:(UIImage *)image? atIndex:(NSUInteger)segment animated:(BOOL)animated
根據(jù)索引刪除標(biāo)簽
- (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated;
刪除所有標(biāo)簽
- (void)removeAllSegments;
重設(shè)標(biāo)簽標(biāo)題
- (void)setTitle:(NSString *)title forSegmentAtIndex:(NSUInteger)segment;
獲取標(biāo)簽標(biāo)題
- (NSString *)titleForSegmentAtIndex:(NSUInteger)segment;
設(shè)置標(biāo)簽圖片
- (void)setImage:(UIImage *)image forSegmentAtIndex:(NSUInteger)segment;
獲取標(biāo)簽圖片
- (UIImage *)imageForSegmentAtIndex:(NSUInteger)segment;
注意:標(biāo)題的圖片只能設(shè)置一個(gè)
根據(jù)索引設(shè)置相應(yīng)標(biāo)簽寬度
- (void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment;
注意:如果設(shè)置為0.0,則為自適應(yīng),默認(rèn)為此設(shè)置。
根據(jù)索引獲取標(biāo)簽寬度
- (CGFloat)widthForSegmentAtIndex:(NSUInteger)segment;
設(shè)置標(biāo)簽內(nèi)容的偏移量
- (void)setContentOffset:(CGSize)offset forSegmentAtIndex:(NSUInteger)segment;
注意:這個(gè)偏移量指的是標(biāo)簽的文字或者圖片
根據(jù)索引獲取變標(biāo)簽內(nèi)容的偏移量
- (CGSize)contentOffsetForSegmentAtIndex:(NSUInteger)segment;
根據(jù)所以設(shè)置標(biāo)簽是否有效(默認(rèn)有效)
- (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment;
根據(jù)索引獲取當(dāng)前標(biāo)簽是否有效
- (BOOL)isEnabledForSegmentAtIndex:(NSUInteger)segment;
設(shè)置和獲取當(dāng)前選中的標(biāo)簽索引
@property(nonatomic) NSInteger selectedSegmentIndex;
設(shè)置標(biāo)簽風(fēng)格顏色
@property(nonatomic,retain) UIColor *tintColor;
注意:這個(gè)風(fēng)格顏色會(huì)影響標(biāo)簽的文字和圖片
設(shè)置特定狀態(tài)下segment的背景圖案
- (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics
獲取背景圖案
- (UIImage *)backgroundImageForState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics
設(shè)置標(biāo)簽之間分割線的圖案
-
(void)setDividerImage:(UIImage *)dividerImage
forLeftSegmentState:(UIControlState)leftState
rightSegmentState:(UIControlState)rightState
barMetrics:(UIBarMetrics)barMetrics
獲取標(biāo)簽之間分割線的圖案
-
(UIImage *)dividerImageForLeftSegmentState:(UIControlState)leftState
rightSegmentState:(UIControlState)rightState
barMetrics:(UIBarMetrics)barMetrics
通過Attribute字符串屬性字典設(shè)置標(biāo)簽標(biāo)題
- (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state
獲取Attribute字符串屬性字典
- (NSDictionary *)titleTextAttributesForState:(UIControlState)state
自行設(shè)置標(biāo)簽內(nèi)容的偏移量
- (void)setContentPositionAdjustment:(UIOffset)adjustment forSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics
注意:UIOffset為偏移量,這個(gè)結(jié)構(gòu)體中又兩個(gè)浮點(diǎn)數(shù),分別表示水平量和豎直量;UISegmentedControlSegment類型參數(shù)是一個(gè)枚舉
獲取自定義偏移量
-(UIOffset)contentPositionAdjustmentForSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone
barMetrics:(UIBarMetrics)barMetrics
添加點(diǎn)擊事件
[segmentedControl addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];