- 创建
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];
- 获取选项个数 (只读)
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;
- 设置元素的色调
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;
- 是否有选中的状态
segmentedControl.momentary = YES; // 默认为NO
- 选项是否可用
// 设置序号所在选项是否可用
-(void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment;
// 获取序号所在选项是否可用
-(BOOL)isEnabledForSegmentAtIndex:(NSUInteger)segment;
- 选项宽度
// 设置序号所在选项宽度
-(void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment;
// 获取序号所在选项宽度
-(CGFloat)widthForSegmentAtIndex:(NSUInteger)segment;
- 是否根据选项中的内容自适应选项宽度
segmentedControl.apportionsSegmentWidthsByContent = YES; // 默认为NO
- 选项标题
// 向序号所在选项设置标题
// 与图片的设置操作互斥
-(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;
⚠️ 注意:这里有个小问题就是,系统会自动将我们设置的图片格式化成系统的风格
- 选项标题富文本属性
// 根据ControlState状态设置选项标题的富文本属性
-(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;
- 选项间分割线图片
// 根据左选中右正常、左正常右选中、左正常右正常等状态以及UIBarMetrics状态设置分割线图片
-(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;
- 选项内容偏移量
// 设置序号所在选项的内容偏移量
-(void)setContentOffset:(CGSize)offset forSegmentAtIndex:(NSUInteger)segment;
// 获取序号所在选项的内容偏移量
-(CGSize)contentOffsetForSegmentAtIndex:(NSUInteger)segment;
[segmentedControl setContentOffset:CGSizeMake(20, 20) forSegmentAtIndex:1];
- 各状态下选项内容的偏移量
// 设置不同状态下选项内容的偏移量
-(void)setContentPositionAdjustment:(UIOffset)adjustment forSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// 获取不同状态下选项内容的偏移量
-(UIOffset)contentPositionAdjustmentForSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
版权声明:出自MajorLMJ技术博客的原创作品 ,转载时必须注明出处及相应链接!