分段控制器

继承自HMSegmentedControl

此文章只讨论我们自己添加的相关属性和方法,更多设置请参考HMSegmentedControl

如无特殊情况,请尽量只在使用我们自己提供的API的前提下完成需求。(使用 HMSegmentedControl 原生参数可能会导致UI不统一)

Dynamic

Fixed

尺寸

宽度: 屏幕宽度,不可修改

高度: 40,固定,不可修改

样式

Dynamic

每个选项区域宽度为文字长度左右各 +edgeMargin

edgeMargin默认为 15

Fixed

每个选项区域宽度为 MAX(屏幕宽度/count, 文字宽度左右各 +edgeMargin)

edgeMargin默认为 0

如无特殊情况,此样式下 edgeMargin 使用 0 即可

代码

Enumeration

HMSegmentedControlSegmentWidthStyle

1
2
3
4
typedef NS_ENUM(NSInteger, HMSegmentedControlSegmentWidthStyle) {
HMSegmentedControlSegmentWidthStyleFixed, // Segment width is fixed
HMSegmentedControlSegmentWidthStyleDynamic, // Segment width will only be as big as the text width (including inset)
};

Class

WDSegmentedView

Property
1
@property (nonatomic, weak) id<WDSegmentedViewDelegate> delegate;

选中的项目文字和指示器的颜色,默认 SMTColorNameLinkColor,可设置

1
@property (nonatomic, strong) UIColor *tintColor;

1
@property (nonatomic, strong, readonly) UIFont *normalTextFont;
1
@property (nonatomic, strong, readonly) UIFont *boldTextFont;
实例方法

指定titles,style默认为 HMSegmentedControlSegmentWidthStyleDynamic,edgeMargin默认为为 15

1
- (instancetype)initWithTitles:(NSArray *)titles;

style需要设置为 HMSegmentedControlSegmentWidthStyleFixed 时,edgeMargin 一般设置为 0

1
- (instancetype)initWithTitles:(NSArray *)titles widthStyle:(HMSegmentedControlSegmentWidthStyle)style edgeMargin:(CGFloat)edgeMargin;

当选择项不只包含文字时使用此方法,需要自行给定合适的 normalImages 和 selectedImages

1
2
3
4
- (instancetype)initWithNormalImages:(NSArray *)normalImages
selectedImages:(NSArray *)selectedImages
widthStyle:(HMSegmentedControlSegmentWidthStyle)widthStyle
edgeMargin:(CGFloat)edgeMargin;

1
- (void)configSegmentedControlWithWidthType:(HMSegmentedControlSegmentWidthStyle)widthStyle edgeMargin:(CGFloat)edgeMargin;

选择项有变动的情况下使用

1
- (void)resetTitles:(NSArray *)titles selectedIndex:(NSInteger)selectedIndex;

Delegate

1
- (void)segmentedView:(WDSegmentedView *)segmentedView didSelecteIndex:(NSUInteger)index;