码迷,mamicode.com
首页 > 其他好文 > 详细

UISwitch(开关控件)、UISegmentedControl(分段控件)

时间:2015-11-14 20:42:18      阅读:342      评论:0      收藏:0      [点我收藏+]

标签:

一、UISwitch
1、初始化
UISwitch *s1 = [[UISwitch alloc]initWithFrame:CGRectMake(50, 170, 100, 200)];
 
2、设置相关属性
s1.onTintColor = [UIColor blueColor];
s1.tintColor = [UIColor greenColor];
s1.thumbTintColor = [UIColor redColor];
3、设置开关状态
s1.on = YES;
4、添加事件
    [s1 addTarget:self action:@selector(myswitch) forControlEvents:UIControlEventValueChanged];
   
    [s1 addTarget:self action:@selector(myswitch:) forControlEvents:UIControlEventValueChanged];
 
5、加入父视图
 
 [self.view addSubview:s1];
 
添加事件的方法
 
  -(void)myswitch{
    NSLog(@"触发了改方法");
}
-(void)myswitch:(UISwitch *)s{
    UISwitch *s2 = (UISwitch *)[self.view viewWithTag:1];
    NSLog(@"%d",s.isOn);
    NSLog(@"%d",s2.isOn);
}
 
 
二、UISegmentedControl
1、初始化
(1)方式1
UISegmentedControl *seg  =[[UISegmentedControl alloc] initWithFrame:CGRectMake(30, 100, 200, 40)];//创建时初始化大小,但是不设置每段的参数
(2)方式2
UISegmentedControl *seg1 = [[UISegmentedControl alloc] initWithItems:@[@"1",@"2",@"3"]];//初始化时直接给出初始的段数及title
2、设置相关属性
(1)[seg setTitle:@"我的" forSegmentAtIndex:0];//设置某一段的标题文字
 
(2)[seg setImage:[UIImage imageNamed:@"lanzuan"] forSegmentAtIndex:0];//设置某一分段的image,注意title和image二者只能存在一个
 
(3)[seg insertSegmentWithTitle:@"2" atIndex:0 animated:NO];//在下标为0的位置插入一个标题为2的分段,注意下标的合理性
 
(4)[seg insertSegmentWithImage:[UIImage imageNamed:@"1.png"] atIndex:0 animated:NO];//在下标为0的位置插入一个分段,设置其图片
 
(5)seg.tintColor = [UIColor whiteColor];//设置segment的边框颜色及文字/图片颜色
 
(6)[seg removeSegmentAtIndex:0 animated:YES];//移除某一分段
 
(7)[seg removeAllSegments];//移除所有的分段
 
(8)seg.numberOfSegments; //移除某一分段
 
(9)seg.selectedSegmentIndex = 0;//默认选中第几个选段
 
(10)NSString *str = [seg titleForSegmentAtIndex:0];//获取某一段的title
 
(11)UIImage  *image = [seg imageForSegmentAtIndex:0];//获取某一段的图片
 
(12)[seg setWidth:30 forSegmentAtIndex:0];//设置某一段的宽度
 
(13)[seg setBackgroundImage:[UIImage imageNamed:@"imsend"] forState: UIControlStateNormal barMetrics: UIBarMetricsDefault];//设置常态下的背景图
 
(14)[seg setBackgroundImage:[UIImage imageNamed:@"logoin_6"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];//设置选中的背景图
 
(15)[seg addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];//绑定监听事件
 
 
 
UISegmentedControl *segment1 = [[UISegmentedControlalloc]initWithItems:@[@"你好",@"欢迎",@"加入"]];//初始化时直接给出初始的段数及title

    segment1.frame = CGRectMake(90, 240, 200, 50);//初始化大小
  
    [segment1 insertSegmentWithImage:[UIImage imageNamed:@"3.jpg"] atIndex:1 animated:YES];//插入图片并允许动画
   
     NSLog(@"段数:%zi",segment1.numberOfSegments);//获取段数
    [segment1 setTitle:@"我们" forSegmentAtIndex:0];
    NSString *str = [segment1 titleForSegmentAtIndex:1];//获取图片标题,注意图片中插入的位置要对应起来
   
    NSLog(@"图片标题=%@",str);
    UIImage *image = [segment1 imageForSegmentAtIndex:1];
    NSLog(@"图片=%@",image);
   
    //[segment1 setWidth:100 forSegmentAtIndex:0];//设置某段的宽
    //[segment1 setEnabled:NO forSegmentAtIndex:0];//设置某一段是否被激活
    //[segment1 setEnabled:NO];//整体不被激活
   
    segment1.selectedSegmentIndex = 1;//设置默认选中项
    NSLog(@"%zi",segment1.selectedSegmentIndex);//获取当前被选中的段
    segment1.tintColor = [UIColor greenColor];//边框和选中颜色
    [segment1 setBackgroundImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    // UIControlStateNormal、UIBarMetricsDefault 设置成默认的就好
    [segment1 addTarget:self action:@selector(segmentchange) forControlEvents:UIControlEventValueChanged];
 
//触发事件
-(void)segmentchange{
    NSLog(@"分段被点击");
}
   
    [self.view addSubview:segment1];
 
   
    UISegmentedControl *s = [[UISegmentedControl alloc]initWithFrame:CGRectMake(50, 400, 100, 50)];
    [s insertSegmentWithTitle:@"分段" atIndex:3 animated:YES];
    [s insertSegmentWithTitle:@"函数" atIndex:0 animated:YES];//增加
    //[s removeSegmentAtIndex:0 animated:YES];//移除
    [self.view addSubview:s];
   

UISwitch(开关控件)、UISegmentedControl(分段控件)

标签:

原文地址:http://www.cnblogs.com/wxzboke/p/4964935.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!