IOS UISegmentedControl

简介:

SegmentedControl又被称作分段控制器,是IOS开发中经常用到的一个UI控件。

初始化方法:传入的数组可以是字符串也可以是UIImage对象的图片数组

- (instancetype)initWithItems:(NSArray *)items;

设置控件风格:

@property(nonatomic) UISegmentedControlStyle segmentedControlStyle

注意:这个属性已经废弃,不再起任何作用,它的枚举如下:

?
1
2
3
4
5
6
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" );

设置是否保持选中状态

@property(nonatomic,getter=isMomentary) BOOL momentary; 

注意:如果设置为YES,点击结束后,将不保持选中状态,默认为NO

获取标签个数:(只读)

@property(nonatomic,readonly) NSUInteger numberOfSegments;

设置标签宽度是否随内容自适应:

@property(nonatomic) BOOL apportionsSegmentWidthsByContent;

注意:如果设置为NO,则所有标签宽度一致,为最大宽度。

插入文字标签在index位置:

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

插入图片标签在index位置

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

根据索引删除标签

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

删除所有标签

- (void)removeAllSegments;

重设标签标题

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

获取标签标题

- (NSString *)titleForSegmentAtIndex:(NSUInteger)segment;

设置标签图片

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

获取标签图片

- (UIImage *)imageForSegmentAtIndex:(NSUInteger)segment;

注意:标题的图片只能设置一个

根据索引设置相应标签宽度

- (void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment; 
注意:如果设置为0.0,则为自适应,默认为此设置。

根据索引获取标签宽度

- (CGFloat)widthForSegmentAtIndex:(NSUInteger)segment;

设置标签内容的偏移量

- (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(nonatomic,retain) UIColor *tintColor;

注意:这个风格颜色会影响标签的文字和图片

设置特定状态下segment的背景图案

- (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics

注意:UIBarMetrics是一个枚举,如下:(defaulf风格会充满背景)

?
1
2
3
4
5
6
7
8
9
typedef  NS_ENUM(NSInteger, UIBarMetrics) {
     UIBarMetricsDefault,
     UIBarMetricsCompact,
     UIBarMetricsDefaultPrompt = 101,  // Applicable only in bars with the prompt property, such as UINavigationBar and UISearchBar
     UIBarMetricsCompactPrompt,
 
     UIBarMetricsLandscapePhone NS_ENUM_DEPRECATED_IOS(5_0, 8_0,  "Use UIBarMetricsCompact instead" ) = UIBarMetricsCompact,
     UIBarMetricsLandscapePhonePrompt NS_ENUM_DEPRECATED_IOS(7_0, 8_0,  "Use UIBarMetricsCompactPrompt" ) = UIBarMetricsCompactPrompt,
};

获取背景图案

- (UIImage *)backgroundImageForState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics

设置标签之间分割线的图案

- (void)setDividerImage:(UIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics

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

- (UIImage *)dividerImageForLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics

通过Attribute字符串属性字典设置标签标题

- (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state

获取Attribute字符串属性字典

- (NSDictionary *)titleTextAttributesForState:(UIControlState)state

自行设置标签内容的偏移量

- (void)setContentPositionAdjustment:(UIOffset)adjustment forSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics 

注意:UIOffset为偏移量,这个结构体中又两个浮点数,分别表示水平量和竖直量;UISegmentedControlSegment类型参数是一个枚举,如下:

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

获取自定义偏移量

- (UIOffset)contentPositionAdjustmentForSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics

添加点击事件

?
1
[segmentedControl addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
目录
相关文章
|
iOS开发
IOS获取 UIWindow
IOS获取 UIWindow
405 0
|
iOS开发 索引
iOS UISegmentedControl
iOS UISegmentedControl
166 0
|
iOS开发 索引
IOS中UIActionSheet使用详解
IOS中UIActionSheet使用详解
255 0
|
iOS开发
iOS UIButton解读
iOS UIButton解读
166 0
|
开发者 iOS开发
iOS-UIPickerView 详解总结
写在文前 由于最近开发中经常碰到类似日期选择器相关业务使用场景,虽然这个系统控件相对来说非常简单,有点儿类似UITableView的感觉,初始化之后设置数据源,代理,完成相应的数据源方法就可以正常展示了,而且其数据源 代理方法相对来说也很少,肯花心思去思考 记忆,很快就能掌握这个控件。
1189 0
|
iOS开发 Swift
iOS - UISegmentedControl
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UISegmentedControl : UIControl @available(iOS 2.0, *) public class UISegmentedControl : UIControl, NSCoding UISegmentedControl 的 Items 有两种样式,一种是字符串设置的标题样式,一种是图片设置的样式。
1122 0
|
iOS开发 Swift
iOS - UISwitch
前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UISwitch : UIControl @available(iOS 2.
1124 0
|
iOS开发 Swift
iOS - UISlider
前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UISlider : UIControl @available(iOS 2.
1189 0
|
iOS开发 Swift
iOS - UIDatePicker
前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIDatePicker : UIControl @available(iOS 2.0, *) public class UIDatePicker : UIControl, NSCoding UIDatePicker 是 UIKit 控件中提供日期和时间选择的控件。
1007 0

热门文章

最新文章