SegmentedControl又被稱作分段控制器,是IOS開發中經常用到的一個UI控件。

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

- (instancetype)initWithItems:(NSArray *)items;

設置控件風格:

@property(nonatomic) UISegmentedControlStyle segmentedControlStyle

注意:這個屬性已經廢棄,不再起任何作用,它的枚舉如下:

 

typedef NS_ENUM(NSInteger, UISegmentedControlStyle) {

UISegmentedControlStylePlain,     // large plain


UISegmentedControlStyleBordered,  // large bordered


UISegmentedControlStyleBar,       // small button/nav bar style. tintable


UISegmentedControlStyleBezeled,   // DEPRECATED. Do not use this style.


} NS_DEPRECATED_IOS(2_0, 7_0, "The segmentedControlStyle property no longer has any effect");

 

設置是否保持選中狀態:

@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

注意:UIBarMetrics是一個枚舉,如下:(defaulf風格會充滿背景)

typedef NS_ENUM(NSInteger, UIBarMetrics) {

UIBarMetricsDefault,


UIBarMetricsCompact,


UIBarMetricsDefaultPrompt = 101, // Applicable only in bars with the prompt property, such as UINavigationBar and UISearchBar


UIBarMetricsCompactPrompt,

 

UIBarMetricsLandscapePhone NS_ENUM_DEPRECATED_IOS(5_0, 8_0, "Use UIBarMetricsCompact instead") = UIBarMetricsCompact,


UIBarMetricsLandscapePhonePrompt NS_ENUM_DEPRECATED_IOS(7_0, 8_0, "Use UIBarMetricsCompactPrompt") = UIBarMetricsCompactPrompt,

};

獲取背景圖案

- (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類型參數是一個枚舉,如下:

typedef NS_ENUM(NSInteger, UISegmentedControlSegment) {

UISegmentedControlSegmentAny = 0,//所有標籤都受影響


UISegmentedControlSegmentLeft = 1,  //只有左邊部分受到影響 


UISegmentedControlSegmentCenter = 2, // 只有中間部分受到影響


UISegmentedControlSegmentRight = 3,  // 只有右邊部分受到影響


UISegmentedControlSegmentAlone = 4,  // 在只有一個標籤的時候生效

};

獲取自定義偏移量

- (UIOffset)contentPositionAdjustmentForSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics

添加點擊事件

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