您现在的位置是:首页 > 文章详情

iOS:分段控件UISegmentedControl的详细使用

日期:2017-06-24点击:410

分段控件:UISegmentedControl

 
功能:分段的控制、页面的切换等。
 
介绍:当用户输入不仅仅是布尔值时,可使用分段控件(UISegmentedControl)。分段控件提供一栏按钮(有时称为按钮栏),但只能激活其中一个按钮。分段控件会导致用户在屏幕上看到的内容发生变化。它们常用于在不同类别的信息之间选择,或在不同的应用屏幕之间切换。下面介绍基本属性和基本方法的使用。继承自UIControl。
 
类型:

typedef NS_ENUM(NSInteger, UISegmentedControlStyle) {

    UISegmentedControlStylePlain,                 // 有灰色的大白按钮,适合偏好设置单元

    UISegmentedControlStyleBordered,           // 黑色边界的大白按钮,适用于表格单元

    UISegmentedControlStyleBar,                  // 小按钮,适合导航栏

    UISegmentedControlStyleBezeled,            //斜角/斜边按钮 

}

enum {

       UISegmentedControlNoSegment = -1        // 不进行分段

};

typedef NS_ENUM(NSInteger, UISegmentedControlSegment) {

    UISegmentedControlSegmentAny = 0,           //所有标签都受影响

    UISegmentedControlSegmentLeft = 1,          //只有左边部分受到影响

    UISegmentedControlSegmentCenter = 2,      //只有中间部分受到影响

    UISegmentedControlSegmentRight = 3,        //只有右边部分受到影响

    UISegmentedControlSegmentAlone = 4,        //在只有一个标签时生效

};

属性:

@property(nonatomic) UISegmentedControlStyle segmentedControlStyle;  //分段控件类型

 

@property(nonatomic,getter=isMomentary) BOOL momentary;                //是否保持选中状态

 

@property(nonatomic,readonly) NSUInteger numberOfSegments;       //标签数量

 

@property(nonatomic) BOOL apportionsSegmentWidthsByContent ;  //设置标签宽度是否随内容自适应

 

@property(nonatomic) NSInteger selected ;            //被选中的标签的索引

 

@property(nonatomic,retain) UIColor *tintColor;     //控件颜色

 

方法:

※初始化方法:传入的数组内容可以是字符串也可以是图像

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

 

※在指定索引处插入标签标题

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

 

※在指定索引处插入标签图像

- (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;

 

※ 获取指定索引处的标签的宽度       

- (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;

 

※设置设定状态下segment的背景图像

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

 

※获取设定的状态下的segment的背景图像

- (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 ;

 

※通过attributes字典设置设定的状态下标签的标题

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

 

※获取attributes字典

- (NSDictionary *)titleTextAttributesForState:(UIControlState)state;

 

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

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

 

※获取自定义的标签内容偏移量

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

 

 具体举例:在ViewController类的- (void)viewDidLoad { [super viewDidLoad];...}方法中添加代码如下:

//1.设置标签内容数组(一个为字符串数组、一个为图像数组)

1 //设置标签单元数组,内容全为文字 2 NSArray *items = @[@"图片",@"视频",@"音乐",@"美食"];
1 //设置标签单元数组,内容全为图像 2 NSArray *images = @[[UIImage imageNamed:@"1.png"],[UIImage imageNamed:@"2.png"],[UIImage imageNamed:@"3.png"]];

//2、创建分段控件实例并设置位置frame

复制代码
//创建分段控件实例 UISegmentedControl *sc = [[UISegmentedControl alloc]initWithItems:items]; //用文字数组初始化

//UISegmentedControl *sc = [[UISegmentedControl alloc]initWithItems:images]; //用图像数组初始化
//设置位置 sc.frame = CGRectMake(60, 120, 200, 50);
复制代码

//3、设置风格(注意:版本升级后,该属性不在有效)

 //设置风格(iOS7.0以后,这个属性不在起任何作用了) //sc.segmentedControlStyle = UISegmentedControlStyleBordered;

//4、设置一开始就选中的标签

 //设置选中的标签 sc.selectedSegmentIndex = 2;

//5、添加控件事件

 //添加控件事件 [sc addTarget:self action:@selector(segmentedChanged:) forControlEvents:UIControlEventValueChanged];

//6、将控件加到视图中

 //将控件添加到视图中 [self.view addSubview:sc];

 触发控件事件:

#pragma mark -segmentedChanged: 触发控件事件

-(void)segmentedChanged:(UISegmentedControl*)sender { NSLog(@"sender: %ld",sender.selectedSegmentIndex); //输出当前的索引值 }

 

演示结果如下:

 当用字符串文字数组初始化,并且没有触发事件时:当前选中的索引是2

     

当触发事件时,当前选中的索引是:0

    

输出结果:     

2015-09-30 23:07:51.026 分段控件UISegmentedControl[5335:319520] sender: 0

 

 当用图像数组初始化时 :

 

 

 

程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!
分类:  iOS初级

本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4850529.html,如需转载请自行联系原作者
原文链接:https://yq.aliyun.com/articles/366577
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章