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