- 創(chuàng)建
UISegmentedControl * segmentedControl = [[UISegmentedControl alloc] init];
UISegmentedControl * segmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(20, 50, 300, 30)];
UISegmentedControl * segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"選項(xiàng)一",@"選項(xiàng)二",@"選項(xiàng)三"]];
- 增、刪選項(xiàng)
// 以標(biāo)題的方式插入一個(gè)選項(xiàng)白粉,動(dòng)畫可選
-(void)insertSegmentWithTitle:(nullable NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated;
// 以圖片的方式插入一個(gè)選項(xiàng)传泊,動(dòng)畫可選
-(void)insertSegmentWithImage:(nullable UIImage *)image atIndex:(NSUInteger)segment animated:(BOOL)animated;
// 刪除序號(hào)所在選項(xiàng),動(dòng)畫可選
-(void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated;
// 刪除所有選項(xiàng)
-(void)removeAllSegments;
[segmentedControl insertSegmentWithTitle:@"選項(xiàng)四" atIndex:3 animated:YES];
- 獲取選項(xiàng)個(gè)數(shù) (只讀)
segmentedControl.numberOfSegments;
- 樣式
?? 該屬性已棄用鸭巴,就不詳細(xì)說了
segmentedControl.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") __TVOS_PROHIBITED;
- 設(shè)置元素的色調(diào)
segmentedControl.tintColor = [UIColor redColor];
- 背景圖片
// 設(shè)置背景圖
-(void)setBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// 獲取背景圖
-(nullable UIImage *)backgroundImageForState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
[segmentedControl setBackgroundImage:[UIImage imageNamed:@"bg"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
- 通過序號(hào)設(shè)置被選中的選項(xiàng)
segmentedControl.selectedSegmentIndex = 1;
- 是否有選中的狀態(tài)
segmentedControl.momentary = YES; // 默認(rèn)為NO
- 選項(xiàng)是否可用
// 設(shè)置序號(hào)所在選項(xiàng)是否可用
-(void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment;
// 獲取序號(hào)所在選項(xiàng)是否可用
-(BOOL)isEnabledForSegmentAtIndex:(NSUInteger)segment;
- 選項(xiàng)寬度
// 設(shè)置序號(hào)所在選項(xiàng)寬度
-(void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment;
// 獲取序號(hào)所在選項(xiàng)寬度
-(CGFloat)widthForSegmentAtIndex:(NSUInteger)segment;
- 是否根據(jù)選項(xiàng)中的內(nèi)容自適應(yīng)選項(xiàng)寬度
segmentedControl.apportionsSegmentWidthsByContent = YES; // 默認(rèn)為NO
- 選項(xiàng)標(biāo)題
// 向序號(hào)所在選項(xiàng)設(shè)置標(biāo)題
// 與圖片的設(shè)置操作互斥
-(void)setTitle:(nullable NSString *)title forSegmentAtIndex:(NSUInteger)segment;
// 獲取序號(hào)所在選項(xiàng)標(biāo)題
-(nullable NSString *)titleForSegmentAtIndex:(NSUInteger)segment;
- 選項(xiàng)圖片
// 向序號(hào)所在選項(xiàng)設(shè)置圖片
// 與標(biāo)題的設(shè)置操作互斥
-(void)setImage:(nullable UIImage *)image forSegmentAtIndex:(NSUInteger)segment;
// 獲取序號(hào)所在選項(xiàng)圖片
-(nullable UIImage *)imageForSegmentAtIndex:(NSUInteger)segment;
?? 注意:這里有個(gè)小問題就是眷细,系統(tǒng)會(huì)自動(dòng)將我們設(shè)置的圖片格式化成系統(tǒng)的風(fēng)格
- 選項(xiàng)標(biāo)題富文本屬性
// 根據(jù)ControlState狀態(tài)設(shè)置選項(xiàng)標(biāo)題的富文本屬性
-(void)setTitleTextAttributes:(nullable NSDictionary *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// 獲取選項(xiàng)標(biāo)題的富文本屬性
-(nullable NSDictionary *)titleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- 選項(xiàng)間分割線圖片
// 根據(jù)左選中右正常、左正常右選中鹃祖、左正常右正常等狀態(tài)以及UIBarMetrics狀態(tài)設(shè)置分割線圖片
-(void)setDividerImage:(nullable UIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// 獲取分割線圖片
-(nullable UIImage *)dividerImageForLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- 選項(xiàng)內(nèi)容偏移量
// 設(shè)置序號(hào)所在選項(xiàng)的內(nèi)容偏移量
-(void)setContentOffset:(CGSize)offset forSegmentAtIndex:(NSUInteger)segment;
// 獲取序號(hào)所在選項(xiàng)的內(nèi)容偏移量
-(CGSize)contentOffsetForSegmentAtIndex:(NSUInteger)segment;
[segmentedControl setContentOffset:CGSizeMake(20, 20) forSegmentAtIndex:1];
- 各狀態(tài)下選項(xiàng)內(nèi)容的偏移量
// 設(shè)置不同狀態(tài)下選項(xiàng)內(nèi)容的偏移量
-(void)setContentPositionAdjustment:(UIOffset)adjustment forSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// 獲取不同狀態(tài)下選項(xiàng)內(nèi)容的偏移量
-(UIOffset)contentPositionAdjustmentForSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
版權(quán)聲明:出自MajorLMJ技術(shù)博客的原創(chuàng)作品 溪椎,轉(zhuǎn)載時(shí)必須注明出處及相應(yīng)鏈接!