UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"1",@"2",@"3"]];
seg.frame = CGRectMake(40, 100, 60, 20);
[seg insertSegmentWithImage:[UIImage imageNamed:@"图片名"] atIndex:4 animated:YES];//给某一段设置图片(图片为带边框透明的才可以完整显示,其它图片不能完整显示)
UIImage *image = [seg imageForSegmentAtIndex:2];//获取某一段的图片
NSLog(@"图片:%@",image);
NSString *str = [seg titleForSegmentAtIndex:1];//获取某一段的标题
NSLog(@“第几个:%zi",segment.selectedSegmentIndex);//获取当前点击的编号
NSLog(@"标题:%@",str);
[seg setWidth:100 forSegmentAtIndex:1];//设置某一段的宽度
[seg setEnabled:YES];//设置整体是否被激活;
[seg setEnabled:YES forSegmentAtIndex:2];//设置某一段是否激活
seg.selectedSegmentIndex = 1;//设置某段为默认选中项
NSLog(@"%zi",seg.selectedSegmentIndex);//获取当前被选中的段;
seg.tintColor = [UIColor purpleColor];//设置边框和选中颜色;
[seg setBackgroundImage:[UIImage imageNamed:@"图片名"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];//设置整体的背景图片
//绑定监听事件
[seg addTarget:self action:@selector(方法名:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:seg];
//第二种创建方式
UISegmentedControl *seg2 = [[UISegmentedControl alloc]initWithFrame:CGRectMake(40, 120, 60, 20)];
[seg2 insertSegmentWithTitle:@"1" atIndex:1 animated:YES];
[seg2 insertSegmentWithTitle:@"2" atIndex:2 animated:YES];
[seg2 removeSegmentAtIndex:1 animated:YES];//移除某段
NSLog(@"%zi",seg2.numberOfSegments);//获取段数
[self.view addSubview:seg2];
}
-(void)mySwitch
{
UISwitch *swi = (UISwitch *)[self.view viewWithTag:1];
NSLog(@"%d",swi.isOn);
}
- (void)viewDidLoad {
[super viewDidLoad];
// 初始化,添加分段名,会自动布局
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"正在销售", @"已下架"]];
self.segmentedControl.frame = CGRectMake(-5, 50, SCREENWIDTH+10, 30);
// 设置整体的色调
self.segmentedControl.tintColor = cmGreen;
// 设置分段名的字体
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:cmGreen,NSForegroundColorAttributeName,[UIFont systemFontOfSize:18],NSFontAttributeName ,nil];
[self.segmentedControl setTitleTextAttributes:dic forState:UIControlStateNormal];
// 设置初始选中项 self.segmentedControl.selectedSegmentIndex = 0;
[self.segmentedControl addTarget:self action:@selector(selectItem:) forControlEvents:UIControlEventValueChanged];
// 添加响应方法
// 设置样式的segmentedControlStyle属性在iOS 7.0之后将不再起作用
// 设置点击后恢复原样,默认为NO,点击后一直保持选中状态
self.segmentedControl.momentary = YES;
// 在指定序号处插入一个分段,会自动布局
[self.segmentedControl insertSegmentWithTitle:@"已删除" atIndex:2 animated:NO];
// 在指定序号处移除一个分段,会自动布局
[self.segmentedControl removeSegmentAtIndex:2 animated:NO];
// 设置指定序号处的分段的宽度
[self.segmentedControl setWidth:50.0 forSegmentAtIndex:1];
// 获取指定序号处标题的内容
NSLog(@"%@", [self.segmentedControl titleForSegmentAtIndex:1]);
// 获取指定序号处标题的宽度
NSLog(@"%f", [self.segmentedControl widthForSegmentAtIndex:1]);
// 设置指定序号处的分段不可选
[self.segmentedControl setEnabled:NO forSegmentAtIndex:1];
[self.view addSubview:self.segmentedControl];}- (void)selectItem:(UISegmentedControl *)sender {
if (sender.selectedSegmentIndex == 0) {
NSLog(@"正在销售");
} else {
NSLog(@"已下架");
}}
//分段控件
- (IBAction)touchDown:(id)sender {
UISegmentedControl *sg = (UISegmentedControl*)sender;
NSLog(@"选择的段:%li",sg.selectedSegmentIndex);//被选的索引selectedSegmentIndex
//如果是隐藏就显示 如果显示就让他隐藏
if (self.LeftSwitch.hidden) {
self.LeftSwitch.hidden = NO;
self.RightSwitch.hidden = NO;
}else{
self.RightSwitch.hidden = YES;
self.LeftSwitch.hidden = YES;
}
}
//eg:
@property(nonatomic,strong)NSArray *dataTestArray;
@property(nonatomic,strong)UISegmentedControl *segement;
@property(nonatomic,strong)UILabel *wan;
@property(nonatomic,strong)UIView *view1;
@property(nonatomic,strong)UIView *view2;
-(NSArray *)dataTestArray{
if (!_dataTestArray) {
_dataTestArray = @[
@"1",
@"2",
@"3"
];
}
return _dataTestArray;
}
UISegmentedControl*segement=[[UISegmentedControl alloc]initWithItems:self.dataTestArray];
segement.tintColor = [UIColor greenColor];
segement.layer.cornerRadius = IphoneSize_Width(5);
[segement addTarget:self action:@selector(segementClick) forControlEvents:UIControlEventValueChanged];
[self.view addSubview: segement];
self.segement = segement;
self.segement.selectedSegmentIndex = 0;
[segement mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(cycleScrollView.mas_bottom).offset(IphoneSize_Height(20));
make.left.equalTo(self.view.mas_left).offset(IphoneSize_Width(10));
make.right.equalTo(self.view.mas_right).offset(-IphoneSize_Width(10));
make.height.offset(IphoneSize_Height(50));
}];
UILabel *wan = [[UILabel alloc]init];
wan.textColor = [UIColor whiteColor];
wan.font = [UIFont systemFontOfSize:15];
wan.text = [self.segement titleForSegmentAtIndex:self.segement.selectedSegmentIndex];
[self.view addSubview:wan];
self.wan = wan;
[wan mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(segement.mas_bottom).offset(IphoneSize_Height(20));
make.centerX.equalTo(self.view);
}];
UIView *view1 = [[UIView alloc]init];
view1.backgroundColor =[UIColor redColor];
[self.view addSubview:view1];
self.view1 = view1;
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(wan.mas_bottom).offset(IphoneSize_Height(10));
make.left.equalTo(self.view.mas_left);
make.right.equalTo(self.view.mas_right);
make.height.offset(IphoneSize_Height(50));
}];
UIView *view2 = [[UIView alloc]init];
view2.backgroundColor =[UIColor blueColor];
view2.hidden = YES;
[self.view addSubview:view2];
self.view2 = view2;
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(wan.mas_bottom).offset(IphoneSize_Height(10));
make.left.equalTo(self.view.mas_left);
make.right.equalTo(self.view.mas_right);
make.height.offset(IphoneSize_Height(50));
}];
- (void)segementClick{
self.wan.text = [self.segement titleForSegmentAtIndex:self.segement.selectedSegmentIndex];
NSLog(@"选择的段:%li",self.segement.selectedSegmentIndex);//被选的索引selectedSegmentIndex
NSLog(@"选择的段title:%@",[self.segement titleForSegmentAtIndex:self.segement.selectedSegmentIndex]);//被选的索引的标题
if (self.segement.selectedSegmentIndex == 0) {
NSLog(@"正在销售");
self.view1.hidden = NO;
self.view2.hidden = YES;
} else {
NSLog(@"已下架");
self.view2.hidden = NO;
self.view1.hidden = YES;
}
}