UISegmentedControl的基本用法

@UISegmentedControl类似于UIButton,它可以提供多个选择操作,响应事件,但具有很大的局限性,我们更多的是使用自定义的,不过在这里还是介绍下它的基本用法.

   NSArray *segmentedArray = [[NSArrayalloc]initWithObjects:@"1",@"2",@"3",@"4",nil];
   //初始化UISegmentedControl
   UISegmentedControl *segmentedControl = [[UISegmentedControlalloc]initWithItems:segmentedArray];
   segmentedControl.frame = CGRectMake(20.0,……)
   // 设置默认选择项索引
   segmentedControl.selectedSegmentIndex = 2;
   segmentedControl.tintColor = [UIColor redColor];
   // 有基本四种样式
   segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
   segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;
   segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
   segmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;

   // 设置在点击后是否恢复原样
    segmentedControl.momentary = YES;
    [segmentedControl setTitle:@"two" forSegmentAtIndex:1];//设置指定索引的题目
    [segmentedControl setImage:[UIImage imageNamed:@"btn_jyy.png"] forSegmentAtIndex:3];//设置指定索引的图片
   // 在指定索引插入一个选项并设置图片
    [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"mei.png"] atIndex:2 animated:NO];
   // 在指定索引插入一个选项并设置题目
    [segmentedControl insertSegmentWithTitle:@"insert" atIndex:3 animated:NO];

   // 移除指定索引的选项
    [segmentedControl removeSegmentAtIndex:0 animated:NO];
   // 设置指定索引选项的宽度
    [segmentedControl setWidth:70.0 forSegmentAtIndex:2];
   // 设置选项中图片等的左上角的位置
    [segmentedControl setContentOffset:CGSizeMake(10.0,10.0) forSegmentAtIndex:4];

    //获取指定索引选项的图片imageForSegmentAtIndex:
    UIImageView *imageForSegmentAtIndex = [[UIImageViewalloc]initWithImage:[segmentedControl imageForSegmentAtIndex:1]];
    imageForSegmentAtIndex.frame = CGRectMake(60.0, 120.0, 30.0, 30.0);  ;

    //获取指定索引选项的标题titleForSegmentAtIndex
    UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(100.0, 160.0, 30.0, 30.0)];
    titleForSegmentAtIndex.text = [segmentedControl titleForSegmentAtIndex:0]; 

    //获取总选项数segmentedControl.numberOfSegments
    UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(140.0, 170.0, 30.0, 30.0)];
    numberOfSegments.text = [NSString stringWithFormat:@"%d",segmentedControl.numberOfSegments];

    //获取指定索引选项的宽度widthForSegmentAtIndex:
    UILabel *widthForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(180.0, 210.0, 70.0, 30.0)];
    widthForSegmentAtIndex.text = [NSString stringWithFormat:@"%f",[segmentedControl widthForSegmentAtIndex:2]]; 

   // [segmentedControl setEnabled:NO forSegmentAtIndex:4];//设置指定索引选项不可选
   // BOOL enableFlag = [segmentedControl isEnabledForSegmentAtIndex:4];//判断指定索引选项是否可选
      [mySegmentedControladdTarget:selfaction:@selector(didClicksegmentedControlAction:)forControlEvents:UIControlEventValueChanged];  

-(void)didClicksegmentedControlAction:(UISegmentedControl *)Seg{
    NSInteger Index = Seg.selectedSegmentIndex;
    NSLog(@"Index %i", Index);
    switch (Index) {
        case 0:
            [self selectmyView1];
            break;
        case 1:
            [self selectmyView2];
            break;
        case 2:
            [self selectmyView3];
            break;
        case 3:
            [self selectmyView4];
            break;
…………………………………….
        default:
            break;
    }
}

UISegmentedControl的基本用法,布布扣,bubuko.com

时间: 2024-10-14 05:40:25

UISegmentedControl的基本用法的相关文章

Swift - 分段选择控件(UISegmentedControl)的用法

1,选择控件的创建,并监听控件选择值 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class ViewController: UIViewController {     override func viewDidLoad() {         super.viewDidLoad()         //选项除了文字还可以是图片         var items=["选项一","选项二",UIIma

IOS UI segmentedControl UISegmentedControl 常见属性和用法

UISegmentedControl中一些常见的属性和用法 //设置以图案作为分段的显示,仅需要图案的轮廓,这样颜色为分段的背景颜色 //    NSArray *items = @[[UIImage imageNamed:@"segmented1"],[UIImage imageNamed:@"segmented_4"],[UIImage imageNamed:@"segmented_3"],[UIImage imageNamed:@"

UISegmentedControl的常用属性和用法

1.UISegmentControl 1)先创建一个数组用来存放标题 NSArray *array = @[@"1",@"2",@"3",nil]; UISegmentedControl *segmented = [[UISegmentedControl alloc] initWithItems:array]; [segmented insertSegmentWithTitle:@"3" atIndex:3 animated:

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 a

swift - UISegmentedControl 的用法

一.创建控件,并监听控件选择值 /*选项除了文字还可以是图片 as关键字的作用就是字面意思:类型转换*/ // let items = ["选项一","选项二",UIImage(named:"hgl.jpeg")!] as [AnyObject] let items = ["选项一","选项二"] let segmented = UISegmentedControl(items:items) // segm

UISegmentedControl 基本用法

http://blog.csdn.net/heng615975867/article/details/43527295 http://blog.csdn.net/gf771115/article/details/7683565 初始化UISegmentedControl NSArray *arr = [[NSArray alloc]initWithObjects:@"轻拍",@"长按",@"清扫",@"旋转",@"捏

UISegmentedControl 功能简单 分析

UISegmentedControl类似于UIButton,它可以提供多个选择操作,响应事件,但具有很大的局限性,我们更多的是使用自定义的,不过在这里还是介绍下它的基本用法. NSArray *segmentedArray = [[NSArrayalloc]initWithObjects:@"1",@"2",@"3",@"4",nil]; //初始化UISegmentedControl UISegmentedControl *

js中获取时间new date()的用法

js中获取时间new date()的用法 获取时间:   var myDate = new Date();//获取系统当前时间 获取特定格式的时间: 1 myDate.getYear(); //获取当前年份(2位) 2 myDate.getFullYear(); //获取完整的年份(4位,1970-????) 3 myDate.getMonth(); //获取当前月份(0-11,0代表1月) 4 myDate.getDate(); //获取当前日(1-31) 5 myDate.getDay();

20.5 Shell脚本中的逻辑判断;20.6 文件目录属性判断;20.7 if特殊用法;20.8 20.9 cace判断(上下)

扩展: select用法 http://www.apelearn.com/bbs/thread-7950-1-1.html 20.5 Shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi 1. 创建if1.sh测试脚本: [[email protected] ~]# vi if1.sh a=5,如果a大于3,满足这个条件,显示ok 添加内容: #!/bin/bash a=5 if [ $a -gt 3 ] then echo ok fi 2. 执行if1.sh脚本: [[e