NSArray *segmentedArray = [NSArrayarrayWithObjects:STR(@"Mynews"),STR(@"Systemmessage"),nil];
UISegmentedControl *segmentedControl = [[UISegmentedControlalloc]initWithItems:segmentedArray];
segmentedControl.backgroundColor = [UIColorkvColorWithRed:21green:33blue:46alpha:1];
segmentedControl.layer.masksToBounds = YES; // 默认为no,不设置则下面一句无效
segmentedControl.layer.cornerRadius = 8; // 设置圆角大小,同UIView
segmentedControl.layer.borderWidth = 1.5; // 边框宽度,重新画边框,若不重新画,可能会出现圆角处无边框的情况
segmentedControl.layer.borderColor = [UIColorblackColor].CGColor; // 边框颜色
segmentedControl.frame = CGRectMake(0, 0.15029*CFG, 0.2716*CFW, 0.0814*CFG); // 0.3642*CFW
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.tintColor = [UIColorkvColorWithRed:252green:108blue:33alpha:1];
// segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColorwhiteColor]}forState:UIControlStateSelected];
[segmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColorkvColorWithRed:129green:129blue:129alpha:1],NSFontAttributeName:[UIFontboldSystemFontOfSize:14.0f]}forState:UIControlStateNormal];
// UIFont *font = [UIFont boldSystemFontOfSize:14.0f]; // 设置字体大小
// NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
// forKey:NSFontAttributeName];
// [segmentedControl setTitleTextAttributes:attributes
// forState:UIControlStateNormal];
[self.viewaddSubview:segmentedControl];
//添加事件
[segmentedControl addTarget:selfaction:@selector(change:) forControlEvents:UIControlEventValueChanged]
}
// 实现添加的事件
//点击不同分段就会有不同的事件进行相应
-(void)change:(UISegmentedControl *)sender{
NSLog(@"测试");
if (sender.selectedSegmentIndex == 0) {
NSLog(@"1");
[self.systemNewsViewremoveFromSuperview];
[self.viewaddSubview:self.myTableView];
[self.myTableViewreloadData];
}elseif (sender.selectedSegmentIndex == 1){
NSLog(@"2");
[self.viewaddSubview:[selfsystemNewsView]];
[self.myTableViewremoveFromSuperview];
[self.systemNewsViewreloadData];
}
}
今天我们来说下iOS中的分段选择控制器UISegmentedControl,这一控件有什么作用呢
- 每个segment都能被点击,相当于集成了多个button
- 通常我们会点击不同的segment来切换不同的view
那么它实现了一个什么效果呢,我们先看下图:
那么怎么实现这一效果能,只需要简单几句代码:
//先生成存放标题的数据
NSArray *array = [NSArray arrayWithObjects:@"家具",@"灯饰",@"建材",@"装饰", nil];
//初始化UISegmentedControl
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:array];
//设置frame
segment.frame = CGRectMake(10, 100, self.view.frame.size.width-20, 30);
segmentedC.backgroundColor = [UIColor whiteColor]; //
segmentedC.layer.masksToBounds = YES; // 默认为no,不设置则下面一句无效
segmentedC.layer.cornerRadius = 12.5; // 设置圆角大小,同UIView
segmentedC.layer.borderWidth = 1; // 边框宽度,重新画边框,若不重新画,可能会出现圆角处无边框的情况
segmentedC.layer.borderColor = [UIColor blueColor].CGColor; // 边框颜色
segmentedC.backgroundColor = [UIColor whiteColor]; //
segmentedC.layer.masksToBounds = YES; // 默认为no,不设置则下面一句无效
segmentedC.layer.cornerRadius = 12.5; // 设置圆角大小,同UIView
segmentedC.layer.borderWidth = 1; // 边框宽度,重新画边框,若不重新画,可能会出现圆角处无边框的情况
segmentedC.layer.borderColor = [UIColor blueColor].CGColor; // 边框颜色
//添加到视图
[self.view addSubview:segment];
这样一个简单的分段控制器就生成了,这样还不能使用,我们实际开发中需要根据它的属性去进一步设置.
UISegmentedControl属性设置
在segment生成后我们可以根据需求在任意位置插入或删除内容
//添加一个分段(在指定下标下插入,并设置动画效果)
[segment insertSegmentWithTitle:@"五金电料" atIndex:2 animated:NO];
//插入图片分段
//[segment insertSegmentWithImage:[UIImage imageNamed:@"需要插入图片的名字"] atIndex:2 animated:YES];
//移除一个分段(根据下标)
//[segme removeSegmentAtIndex:0 animated:YES];
我们还可以设置其他属性
//根据下标修改分段标题(修改下标为2的分段)
[segme setTitle:@"巧克力" forSegmentAtIndex:2];
//根据内容定分段宽度
segme.apportionsSegmentWidthsByContent = YES;
//开始时默认选中下标(第一个下标默认是0)
segme.selectedSegmentIndex = 2;
//控件渲染色(也就是外观字体颜色)
segment.tintColor = [UIColor redColor];
//按下是否会自动释放:
//segment.momentary = YES;
除了以上属性,我们也可以设置每一个分段的属性
// 设置指定索引选项的宽度(设置下标为2的分段宽度)
[segment setWidth:70.0 forSegmentAtIndex:2];
// 设置分段中标题的位置(0,0点为中心)
[segment setContentOffset:CGSizeMake(10,10) forSegmentAtIndex:3];
当然最重要的是我们要给segment添加事件
//添加事件
[segme addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
实现添加的事件
//点击不同分段就会有不同的事件进行相应
-(void)change:(UISegmentedControl *)sender{
NSLog(@"测试");
if (sender.selectedSegmentIndex == 0) {
NSLog(@"1");
}else if (sender.selectedSegmentIndex == 1){
NSLog(@"2");
}else if (sender.selectedSegmentIndex == 2){
NSLog(@"3");
}else if (sender.selectedSegmentIndex == 3){
NSLog(@"4");
}
如果开发中有需求,我们可以让其在导航栏显示,只需要我们稍微改变下添加到视图的方法
//显示在导航栏上
self.navigationItem.titleView = segmentedControl;
这只是我个人总结的UISegmentedControl的一些简单使用方法,如果有什么问题,还希望大家积极指出,共同进步,谢谢.
文/呼噜ZR(简书作者)
原文链接:http://www.jianshu.com/p/7d9e4d4368c8
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
-
初始化的方法
//一次性初始化多个control,items可以是图片或者是文字array,系统会自动控制每个单元的大小 //也可通过正常View的初始化进行 - (instancetype)initWithItems:(NSArray *)items;
-
事件添加
//通过UIControl的方法来设置,当改变segments的时候通过事件UIControlEventValueChanged, //通过action来处理事件 [self.mySegmentedControl addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventValueChanged];
-
Segmented Controls的内容设置
//通过索引设置以及获取Title和Image - (void)setTitle:(NSString *)title forSegmentAtIndex:(NSUInteger)segment; - (NSString *)titleForSegmentAtIndex:(NSUInteger)segment; - (void)setImage:(UIImage *)image forSegmentAtIndex:(NSUInteger)segment; - (UIImage *)imageForSegmentAtIndex:(NSUInteger)segment;
-
Segmented Controls Segments的设置
//在索引点插入一个新的标题的segment - (void)insertSegmentWithTitle:(NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated; //在索引点插入一个新的图片的segment - (void)insertSegmentWithImage:(UIImage *)image atIndex:(NSUInteger)segment animated:(BOOL)animated; - (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated; - (void)removeAllSegmens; //Segment的数量 @property(nonatomic,readonly) NSUInteger numberOfSegments; //被选中的索引 @property(nonatomic) NSInteger selectedSegmentIndex;
-
Segmented Controls显示和行为设置
//默认值为NO,即点击后保持选中状态,如果设置为YES则,只是短暂的显示选中状态 @property(nonatomic,getter=isMomentary) BOOL momentary; //通过索引调整每个segment的width,如果设置为0.0则表示自动调整大小,默认为0.0 - (void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment; - (CGFloat)widthForSegmentAtIndex:(NSUInteger)segment; //通过索引调整文字或者图片在segment的offset,默认为(0,0) - (void)setContentOffset:(CGSize)offset forSegmentAtIndex:(NSUInteger)segment; - (CGSize)contentOffsetForSegmentAtIndex:(NSUInteger)segment; //设置segment是否能选中 - (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment; - (BOOL)isEnabledForSegmentAtIndex:(NSUInteger)segment; //是否根据内容的大小自动调整宽度 @property(nonatomic) BOOL apportionsSegmentWidthsByContent NS_AVAILABLE_IOS(5_0);
-
自定义的外观
//设置UISegmentedControl的边框,分隔线,文字,点击后的颜色 @property(nonatomic,retain) UIColor *tintColor; //获取和设置背景图片,通过UIControlState和barMetrics的进行设置和获取,主要需要先设置 //UIControlStateNormal才会有效果 - (UIImage *)backgroundImageForState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics - (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics //设置分割线的图片样式,首先需要设置UIControlStateNormal不管左边还是右边, //设置完成后再调用设置方法,根据UIControlState显示分割线 - (void)setDividerImage:(UIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics - (UIImage *)dividerImageForLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetric //设置Title的样式,包括 font, text color, shadow。通过NSAttributedString.h里面的键值对 - (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; - (NSDictionary *)titleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; //调整title和image的位置,通过给定的segment - (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;