IOS ——UI篇—— UISegmentedControl的用法总结
1 //第一种创建方式 2 UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"1",@"2",@"3"]]; 3 seg.frame = CGRectMake(40, 100, 60, 20); 4 5 [seg insertSegmentWithImage:[UIImage imageNamed:@"图片名"] atIndex:4 animated:YES];//给某一段设置图片(图片为带边框透明的才可以完整显示,其它图片不能完整显示) 6 UIImage *image = [seg imageForSegmentAtIndex:2];//获取某一段的图片 7 NSLog(@"图片:%@",image); 8 NSString *str = [seg titleForSegmentAtIndex:1];//获取某一段的标题 9 NSLog(@“第几个:%zi",segment.selectedSegmentIndex);//获取当前点击的编号 10 NSLog(@"标题:%@",str); 11 [seg setWidth:100 forSegmentAtIndex:1];//设置某一段的宽度 12 [seg setEnabled:YES];//设置整体是否被激活; 13 [seg setEnabled:YES forSegmentAtIndex:2];//设置某一段是否激活 14 seg.selectedSegmentIndex = 1;//设置某段为默认选中项 15 NSLog(@"%zi",seg.selectedSegmentIndex);//获取当前被选中的段; 16 seg.tintColor = [UIColor purpleColor];//设置边框和选中颜色; 17 [seg setBackgroundImage:[UIImage imageNamed:@"图片名"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];//设置整体的背景图片 18 19 //绑定监听事件 20 [seg addTarget:self action:@selector(方法名:) forControlEvents:UIControlEventValueChanged]; 21 [self.view addSubview:seg]; 22 23 //第二种创建方式 24 UISegmentedControl *seg2 = [[UISegmentedControl alloc]initWithFrame:CGRectMake(40, 120, 60, 20)]; 25 [seg2 insertSegmentWithTitle:@"1" atIndex:1 animated:YES]; 26 [seg2 insertSegmentWithTitle:@"2" atIndex:2 animated:YES]; 27 [seg2 removeSegmentAtIndex:1 animated:YES];//移除某段 28 NSLog(@"%zi",seg2.numberOfSegments);//获取段数 29 [self.view addSubview:seg2]; 30 31 } 32 33 -(void)mySwitch 34 { 35 UISwitch *swi = (UISwitch *)[self.view viewWithTag:1]; 36 NSLog(@"%d",swi.isOn); 37 38 }
感谢您的访问!
若对您有帮助或有兴趣请关注博客:http://www.cnblogs.com/Rong-Shengcom/