IOS的UISegmentedControl用法
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];