iOS控件之UISegmentedControl

  • 創(chuàng)建
UISegmentedControl * segmentedControl = [[UISegmentedControl alloc] init];
UISegmentedControl * segmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(20, 50, 300, 30)];
UISegmentedControl * segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"選項一",@"選項二",@"選項三"]];
  • 增、刪選項
// 以標題的方式插入一個選項,動畫可選
-(void)insertSegmentWithTitle:(nullable NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated;
// 以圖片的方式插入一個選項,動畫可選
-(void)insertSegmentWithImage:(nullable UIImage *)image  atIndex:(NSUInteger)segment animated:(BOOL)animated;
// 刪除序號所在選項,動畫可選
-(void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated;
// 刪除所有選項
-(void)removeAllSegments;
[segmentedControl insertSegmentWithTitle:@"選項四" atIndex:3 animated:YES];
  • 獲取選項個數(shù) (只讀)
segmentedControl.numberOfSegments;
  • 樣式
?? 該屬性已棄用,就不詳細說了
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;
  • 設置元素的色調(diào)
segmentedControl.tintColor = [UIColor redColor];
  • 背景圖片
// 設置背景圖
-(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];
原圖

  • 通過序號設置被選中的選項
segmentedControl.selectedSegmentIndex = 1;
  • 是否有選中的狀態(tài)
segmentedControl.momentary = YES; // 默認為NO
點擊過后不保留選中的深色狀態(tài)
  • 選項是否可用
// 設置序號所在選項是否可用
-(void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment;
// 獲取序號所在選項是否可用
-(BOOL)isEnabledForSegmentAtIndex:(NSUInteger)segment;
  • 選項寬度
// 設置序號所在選項寬度
-(void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment;
// 獲取序號所在選項寬度
-(CGFloat)widthForSegmentAtIndex:(NSUInteger)segment;
  • 是否根據(jù)選項中的內(nèi)容自適應選項寬度
segmentedControl.apportionsSegmentWidthsByContent = YES; // 默認為NO
默認設置時

設置為YES后
  • 選項標題
// 向序號所在選項設置標題
// 與圖片的設置操作互斥
-(void)setTitle:(nullable NSString *)title forSegmentAtIndex:(NSUInteger)segment;
// 獲取序號所在選項標題
-(nullable NSString *)titleForSegmentAtIndex:(NSUInteger)segment;
  • 選項圖片
// 向序號所在選項設置圖片
// 與標題的設置操作互斥
-(void)setImage:(nullable UIImage *)image forSegmentAtIndex:(NSUInteger)segment;
// 獲取序號所在選項圖片
-(nullable UIImage *)imageForSegmentAtIndex:(NSUInteger)segment;

?? 注意:這里有個小問題就是,系統(tǒng)會自動將我們設置的圖片格式化成系統(tǒng)的風格


原圖

設置到選項中的圖片效果
  • 選項標題富文本屬性
// 根據(jù)ControlState狀態(tài)設置選項標題的富文本屬性
-(void)setTitleTextAttributes:(nullable NSDictionary *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// 獲取選項標題的富文本屬性
-(nullable NSDictionary *)titleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
  • 選項間分割線圖片
// 根據(jù)左選中右正常、左正常右選中、左正常右正常等狀態(tài)以及UIBarMetrics狀態(tài)設置分割線圖片
-(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;
  • 選項內(nèi)容偏移量
// 設置序號所在選項的內(nèi)容偏移量
-(void)setContentOffset:(CGSize)offset forSegmentAtIndex:(NSUInteger)segment;
// 獲取序號所在選項的內(nèi)容偏移量
-(CGSize)contentOffsetForSegmentAtIndex:(NSUInteger)segment;
[segmentedControl setContentOffset:CGSizeMake(20, 20) forSegmentAtIndex:1];
  • 各狀態(tài)下選項內(nèi)容的偏移量
// 設置不同狀態(tài)下選項內(nèi)容的偏移量
-(void)setContentPositionAdjustment:(UIOffset)adjustment forSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; 
// 獲取不同狀態(tài)下選項內(nèi)容的偏移量
-(UIOffset)contentPositionAdjustmentForSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

版權聲明:出自MajorLMJ技術博客的原創(chuàng)作品 ,轉載時必須注明出處及相應鏈接!

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容