×

UISegmentedControl

96
无邪8
2018.05.22 15:09* 字数 1332

一、简介
<<分段控件提供一栏按钮(有时称为按钮栏),但只能激活其中一个按钮。分段控件会导致用户在屏幕上看到的内容发生变化。它们常用于在不同类别的信息之间选择,或在不同的应用屏幕之间切换

<<继承关系:UISegmentedControl-->UIControl-->UIView-->UIResponder-->NSObject

格式为

1-->初始化(作用)

typedef enum {
UISegmentedControlStylePlain, // large plain 有灰边的大白按钮,适合偏好设置单元
UISegmentedControlStyleBordered, // large bordered 黑边的大白按钮,适用于表格单元
UISegmentedControlStyleBar, // small button/nav bar style. tintable 小按钮,适合导航栏
UISegmentedControlStyleBezeled, // large bezeled style. tintable
} UISegmentedControlStyle;(如果属性有枚举类型的话,这里会有枚举类型说明)

mySegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;//风格 (这是具体的例子)

@property(nonatomic) UISegmentedControlStyle segmentedControlStyle NS_DEPRECATED_IOS(2_0, 7_0, "The segmentedControlStyle property no longer has any effect") __TVOS_PROHIBITED;//废弃属性 (这是说明)

二、UISegmentedControl的属性(属性的顺序与苹果API一致)

1--> 初始化方法

NSArray *array = [NSArray arrayWithObjects:@"家具",@"灯饰",@"建材",@"装饰", nil];
UISegmentedControl *segment =[[UISegmentedControl alloc]initWithItems:array];

-(instancetype)initWithItems:(nullable NSArray *)items; // items 可以是 NSStrings or UIImages. 控件的大小会自动调整为适合的内容。

2-->设置UISegmentedControl的风格

typedef enum {
UISegmentedControlStylePlain, // large plain 有灰边的大白按钮,适合偏好设置单元
UISegmentedControlStyleBordered, // large bordered 黑边的大白按钮,适用于表格单元
UISegmentedControlStyleBar, // small button/nav bar style. tintable 小按钮,适合导航栏
UISegmentedControlStyleBezeled, // large bezeled style. tintable
} UISegmentedControlStyle;

mySegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;//风格

@property(nonatomic) UISegmentedControlStyle segmentedControlStyle NS_DEPRECATED_IOS(2_0, 7_0, "The segmentedControlStyle property no longer has any effect") __TVOS_PROHIBITED;//废弃属性

3-->设置在点击后是否恢复原样

mySegmentedControl.momentary = YES;

@property(nonatomic,getter=isMomentary) BOOL momentary; //如果设置,则在跟踪结束后,我们不会继续显示选定的状态。默认是 NO

4-->获取总选项数

[segment numberOfSegments];//得到segment的数量

@property(nonatomic,readonly) NSUInteger numberOfSegments;

5-->是否根据segment的内容改变segment的宽度

segment.apportionsSegmentWidthsByContent = YES;

@property(nonatomic) BOOL apportionsSegmentWidthsByContent NS_AVAILABLE_IOS(5_0);

6-->在指定索引插入一个选项并设置题目

[segmentedControl insertSegmentWithTitle:@"insert" atIndex:3 animated:NO];

-(void)insertSegmentWithTitle:(nullable NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated;

7-->在指定索引插入一个选项并设置图片

[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"mei.png"] atIndex:2 animated:NO];

-(void)insertSegmentWithImage:(nullable UIImage *)image atIndex:(NSUInteger)segment animated:(BOOL)animated;

8-->移除指定索引的选项

[segmentedControl removeSegmentAtIndex:0 animated:NO];

-(void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated;

9-->移出所有segment

[segment removeAllSegments];

-(void)removeAllSegments;

10-->设置指定索引的题目

[segmentedControl setTitle:@"two" forSegmentAtIndex:1];

-(void)setTitle:(nullable NSString *)title forSegmentAtIndex:(NSUInteger)segment;

11-->获取指定索引选项的标题

NSString* myTitle = [mySegmentedControl titleForSegmentAtIndex:1];

-(nullable NSString *)titleForSegmentAtIndex:(NSUInteger)segment;
12-->设置指定索引的图片

segmentedControl setImage:[UIImage imageNamed:@"btn_jyy.png"] forSegmentAtIndex:3];

-(void)setImage:(nullable UIImage *)image forSegmentAtIndex:(NSUInteger)segment;

13-->获取指定索引选项的图片

UIImage* myImage = [mySegmentedControl imageForSegmentAtIndex:2];

-(nullable UIImage *)imageForSegmentAtIndex:(NSUInteger)segment;
14-->设置Item的宽度

[segmentedControl setWidth:70.0 forSegmentAtIndex:2];

-(void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment;
15-->获取指定索引选项的宽度

CGFloat float =[segmentedControl widthForSegmentAtIndex:2];

-(CGFloat)widthForSegmentAtIndex:(NSUInteger)segment;
16-->设置内容偏移

[segment setContentOffset:CGSizeMake(10, 10) forSegmentAtIndex:2];

-(void)setContentOffset:(CGSize)offset forSegmentAtIndex:(NSUInteger)segment;
17-->获取内容偏移

CGSize size = [mySegmentedControl contentOffsetForSegmentAtIndex:2];

-(CGSize)contentOffsetForSegmentAtIndex:(NSUInteger)segment;
18-->设置segment是否可用

[segmentedControl setEnabled:NO forSegmentAtIndex:4];

-(void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment; // default is YES

19-->判断指定索引选项是否可选

BOOL enableFlag = [segmentedControl isEnabledForSegmentAtIndex:4];

-(BOOL)isEnabledForSegmentAtIndex:(NSUInteger)segment;

20-->设置默认选择项索引

segmentedControl.selectedSegmentIndex = 2;

@property(nonatomic) NSInteger selectedSegmentIndex;

21-->设置segments的颜色

segmentedControl.tintColor = [UIColor redColor];

@property(null_resettable,nonatomic,strong) UIColor *tintColor;
22-->设置背景图

typedef enum {
UIBarMetricsDefault, //竖屏
UIBarMetricsLandscapePhone, 横屏
} UIBarMetrics;

[segment setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

-(void)setBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

23-->获取背景图

UIImage* myImage = [mySegmentedControl backgroundImageForState: UIControlStateNormal barMetrics: UIBarMetricsDefault];

-(nullable UIImage *)backgroundImageForState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
24-->设置标签之间分割线的图案

typedef enum {
UIBarMetricsDefault, //竖屏
UIBarMetricsLandscapePhone, 横屏
} UIBarMetrics;

[segment setDividerImage:[UIImage imageNamed:@"234"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

-(void)setDividerImage:(nullable UIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

25-->获取标签之间分割线的图案

UIImage* myImage = [mySegmentedControl dividerImageForLeftSegmentState: UIControlStateNormal
rightSegmentState: UIControlStateNormal
barMetrics: UIBarMetricsDefault];

-(nullable UIImage *)dividerImageForLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
26-->通过Attribute字符串属性字典设置标签标题

[segC setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];

-(void)setTitleTextAttributes:(nullable NSDictionary *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
27-->获取Attribute字符串属性字典

NSDictionary *dic=[segmentedControl titleTextAttributesForState: UIControlStateNormal];

-(nullable NSDictionary *)titleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

28-->设置标签内容的偏移量

typedef NS_ENUM(NSInteger, UISegmentedControlSegment) {
UISegmentedControlSegmentAny = 0,//所有标签都受影响
UISegmentedControlSegmentLeft = 1, //只有左边部分受到影响
UISegmentedControlSegmentCenter = 2, // 只有中间部分受到影响
UISegmentedControlSegmentRight = 3, // 只有右边部分受到影响
UISegmentedControlSegmentAlone = 4, // 在只有一个标签的时候生效
};

[segC setContentPositionAdjustment:UIOffsetMake(0, -60) forSegmentType: UISegmentedControlSegmentAlone
barMetrics: UIBarMetricsDefault];

-(void)setContentPositionAdjustment:(UIOffset)adjustment forSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
29-->获取自定义偏移量

UIOffset offset= [segC s contentPositionAdjustmentForSegmentType: : UISegmentedControlSegmentAlone
barMetrics: UIBarMetricsDefault];

-(UIOffset)contentPositionAdjustmentForSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

ios开发控件
Web note ad 1