iOS-UIKit框架学习—UISegmentedControl

一个UISegmentedControl对象是横向控制多个段,每个段作为一个独立的按钮运作。分段控制提供一个紧凑的手段,组合到一起的控制。

NS_CLASS_AVAILABLE_IOS(2_0) @interface UISegmentedControl : UIControl 

// 初始化方法:传入的数组可以是字符串也可以是UIImage对象的图片数组
- (instancetype)initWithItems:(nullable NSArray *)items;
// 设置是否保持选中状态
@property(nonatomic,getter=isMomentary) BOOL momentary;
// 获取标签个数
@property(nonatomic,readonly) NSUInteger numberOfSegments;
// 设置标签宽度是否随内容自适应 默认是 NO.
@property(nonatomic) BOOL apportionsSegmentWidthsByContent NS_AVAILABLE_IOS(5_0);
// 插入文字标签在index位置
- (void)insertSegmentWithTitle:(nullable NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated;
// 插入图片标签在index位置
- (void)insertSegmentWithImage:(nullable UIImage *)image  atIndex:(NSUInteger)segment animated:(BOOL)animated;
// 根据索引删除标签
- (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated;
// 删除所有标签
- (void)removeAllSegments;
// 重设标签标题
- (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;
// 根据索引设置相应标签宽度 设置为0则自动获取,默认值0
- (void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment;
// 根据索引获取标签宽度
- (CGFloat)widthForSegmentAtIndex:(NSUInteger)segment;
// 设置标签内容的偏移量 默认值0
- (void)setContentOffset:(CGSize)offset forSegmentAtIndex:(NSUInteger)segment;
// 根据索引获取变标签内容的偏移量
- (CGSize)contentOffsetForSegmentAtIndex:(NSUInteger)segment;
// 根据所以设置标签是否有效(默认有效)
- (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment;
// 根据索引获取当前标签是否有效
- (BOOL)isEnabledForSegmentAtIndex:(NSUInteger)segment;
// 设置和获取当前选中的标签索引
@property(nonatomic) NSInteger selectedSegmentIndex;
// 设置标签风格颜色
@property(null_resettable,nonatomic,strong) UIColor *tintColor;
// 置特定状态下segment的背景图案
- (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;
// 设置标签之间分割线的图案
- (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;
// 通过Attribute字符串属性字典设置标签标题
- (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;
// 获取Attribute字符串属性字典
- (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;

@end
enum {
    UISegmentedControlNoSegment = -1   // 没有选定分段的下标
};

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

e.g.

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:@[@"1",@"2",@"3"]];
segmentedControl.frame =  CGRectMake(0, 0, 150, 44);
segmentedControl.tintColor = [UIColor redColor];
segmentedControl.apportionsSegmentWidthsByContent = YES;
segmentedControl.selectedSegmentIndex = 2;
[segmentedControl addTarget:self action:@selector(clickItem:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = segmentedControl;

你可能感兴趣的:(iOS-UIKit框架学习—UISegmentedControl)