UISegment屬性
1.segmentedControlStyle
設定segment的顯示樣式。
typedef NS_ENUM(NSInteger, UISegmentedControlStyle) {
UISegmentedControlStylePlain, // large plain 系統預設平板樣式
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
UISegmentedControlStyleBordered, // large bordered 黑邊樣式
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;
UISegmentedControlStyleBar, // small button/nav bar style. Tintable 條狀樣式
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
UISegmentedControlStyleBezeled, // DEPRECATED. Do not use this style. 這個型別不要使用,用了會報錯喔。
};
2.tintColor 漸變顏色
Default tintColor is nil. Only used if style is UISegmentedControlStyleBar
預設空,只有使用UISegmentedControlStyleBar,才能設定漸變顏色。
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor redColor];
效果:
3.momentary 設定在點選後是否恢復原樣
預設為NO
segmentedControl.momentary = No;
點選之後效果:
segmentedControl.momentary = YES;
點選之後效果:
4. numberOfSegments(只讀)
獲取總選項數segmentedControl.numberOfSegments
5. selectedSegmentIndex
用來設定選中項或者返回選中項。
segmentedControl.selectedSegmentIndex = 2;//設定預設選擇項索引
segmentedControl.selectedSegmentIndex // 獲取選中項
6.- (void)setTitle:(NSString *)title forSegmentAtIndex:(NSUInteger)segment;
[segmentedControl setTitle:@"two" forSegmentAtIndex:1];//設定指定索引的題目
效果:
7. - (void)setImage:(UIImage *)image forSegmentAtIndex:(NSUInteger)segment;
[segmentedControl setImage:[UIImage imageNamed:@"lan.png"] forSegmentAtIndex:3];//設定指定索引的圖片
8.-(void)insertSegmentWithTitle:(NSString*)title atIndex:(NSUInteger)segment animated:(BOOL)animated;
[segmentedControl insertSegmentWithTitle:@"add" atIndex:3 animated:NO];//在指定索引插入一個選項並設定題目
效果:
9.-(void)insertSegmentWithImage:(UIImage *)image atIndex:(NSUInteger)segment animated:(BOOL)animated;
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"mei.png"] atIndex:2 animated:NO];//在指定索引插入一個選項並設定圖片
10.- (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated;
[segmentedControl removeSegmentAtIndex:0 animated:NO];//移除指定索引的選項
效果:
11. - (void)removeAllSegments;
//移除所有選項
[segmentedControl removeAllSegments];
12. - (void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment; // set to 0.0 width to autosize. default is 0.0
選項卡的寬度預設為0,此方法能夠設定選項卡寬度。
[segmentedControl setWidth:70.0 forSegmentAtIndex:2];//設定指定索引選項的寬度
效果:
13. - (void)setContentOffset:(CGSize)offset forSegmentAtIndex:(NSUInteger)segment; // adjust offset of image or text inside the segment. default is (0,0)
[segmentedControl setContentOffset:CGSizeMake(10,0) forSegmentAtIndex:1];
設定選項卡內部文字或者圖片與預設位置的偏移量,預設位置在選項卡的中心。
效果:
14. - (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment;
[segmentedControl setEnabled:NO forSegmentAtIndex:4];//設定指定索引選項不可選
15.增加事件響應機制
監聽的是這個事件:UIControlEventValueChanged,值改變事件
[segmentedControl addTarget:self action:@selector(itemClick) forControlEvents:UIControlEventValueChanged];