UISegment屬性ide
1.segmentedControlStylethis
設置segment的顯示樣式。spa
typedef NS_ENUM(NSInteger, UISegmentedControlStyle) {索引
UISegmentedControlStylePlain, // large plain 系統默認平板樣式事件
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;圖片
UISegmentedControlStyleBordered, // large bordered 黑邊樣式rem
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;get
UISegmentedControlStyleBar, // small button/nav bar style. Tintable 條狀樣式it
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;io
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];