• ios UISegmentedControl 用法 【转】


    NSArray * arr=[NSArrayarrayWithObjects:@"11",@"22",@"33",nil];

       UISegmentedControl * seg=[[UISegmentedControlalloc]initWithItems:arr];

        [self.viewaddSubview:seg];

        

        NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",nil];  

       //初始化UISegmentedControl  

        UISegmentedControl *segmentedControl = [[UISegmentedControlalloc]initWithItems:segmentedArray];  

        segmentedControl = segmentedTemp;  

        segmentedControl.frame =CGRectMake(60.0,10.0,200.0,50.0);  

        

        [segmentedControlsetTitle:@"two"forSegmentAtIndex:1];//设置指定索引的题目  

        [segmentedControlsetImage:[UIImageimageNamed:@"lan.png"]forSegmentAtIndex:3];//设置指定索引的图片  

        [segmentedControlinsertSegmentWithImage:[UIImageimageNamed:@"mei.png"]atIndex:2animated:NO];//在指定索引插入一个选项并设置图片  

        [segmentedControlinsertSegmentWithTitle:@"insert"atIndex:3animated:NO];//在指定索引插入一个选项并设置题目  

        [segmentedControlremoveSegmentAtIndex:0animated:NO];//移除指定索引的选项  

        [segmentedControlsetWidth:70.0forSegmentAtIndex:2];//设置指定索引选项的宽度  

        [segmentedControlsetContentOffset:CGSizeMake(10.0,10.0)forSegmentAtIndex:1];//设置选项中图片等的左上角的位置  

        

       //获取指定索引选项的图片imageForSegmentAtIndex:  

        UIImageView *imageForSegmentAtIndex = [[UIImageViewalloc]initWithImage:[segmentedControlimageForSegmentAtIndex:1]];  

        imageForSegmentAtIndex.frame =CGRectMake(60.0,100.0,30.0,30.0);  

        

       //获取指定索引选项的标题titleForSegmentAtIndex  

        UILabel *titleForSegmentAtIndex = [[UILabelalloc]initWithFrame:CGRectMake(100.0,100.0,30.0,30.0)];  

        titleForSegmentAtIndex.text = [segmentedControltitleForSegmentAtIndex:0];  

        

       //获取总选项数segmentedControl.numberOfSegments  

        UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(140.0,100.0,30.0,30.0)];  

        numberOfSegments.text = [NSStringstringWithFormat:@"%d",segmentedControl.numberOfSegments];  

        

       //获取指定索引选项的宽度widthForSegmentAtIndex:  

        UILabel *widthForSegmentAtIndex = [[UILabelalloc]initWithFrame:CGRectMake(180.0,100.0,70.0,30.0)];  

        widthForSegmentAtIndex.text = [NSStringstringWithFormat:@"%f",[segmentedControlwidthForSegmentAtIndex:2]];  

        

        segmentedControl.selectedSegmentIndex =2;//设置默认选择项索引  

        segmentedControl.tintColor = [UIColorredColor];  

        segmentedControl.segmentedControlStyle =UISegmentedControlStylePlain;//设置样式  

        segmentedControl.momentary =YES;//设置在点击后是否恢复原样  

        

        [segmentedControlsetEnabled:NOforSegmentAtIndex:4];//设置指定索引选项不可选  

        BOOL enableFlag = [segmentedControl isEnabledForSegmentAtIndex:4];//判断指定索引选项是否可选  

        NSLog(@"%d",enableFlag);  

        

        [self.viewaddSubview:widthForSegmentAtIndex];  

        [self.viewaddSubview:numberOfSegments];  

        [self.viewaddSubview:titleForSegmentAtIndex];  

        [self.viewaddSubview:imageForSegmentAtIndex];  

        [self.viewaddSubview:segmentedControl];  

        

        [widthForSegmentAtIndexrelease];  

        [numberOfSegmentsrelease];  

        [titleForSegmentAtIndexrelease];  

        [segmentedTemp release];  

        [imageForSegmentAtIndexrelease];  

    第二种

    UISegmentedControl

    1. *********************************************/  
    2. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.  
    3. - (void)viewDidLoad {  
    4.     NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",nil];  
    5.     //初始化UISegmentedControl  
    6.     UISegmentedControl *segmentedTemp = [[UISegmentedControl alloc]initWithItems:segmentedArray];  
    7.     segmentedControl = segmentedTemp;  
    8.     segmentedControl.frame = CGRectMake(60.0, 10.0, 200.0, 50.0);  
    9.       
    10.     [segmentedControl setTitle:@"two" forSegmentAtIndex:1];//设置指定索引的题目  
    11.     [segmentedControl setImage:[UIImage imageNamed:@"lan.png"] forSegmentAtIndex:3];//设置指定索引的图片  
    12.     [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"mei.png"] atIndex:2 animated:NO];//在指定索引插入一个选项并设置图片  
    13.     [segmentedControl insertSegmentWithTitle:@"insert" atIndex:3 animated:NO];//在指定索引插入一个选项并设置题目  
    14.     [segmentedControl removeSegmentAtIndex:0 animated:NO];//移除指定索引的选项  
    15.     [segmentedControl setWidth:70.0 forSegmentAtIndex:2];//设置指定索引选项的宽度  
    16.     [segmentedControl setContentOffset:CGSizeMake(10.0,10.0) forSegmentAtIndex:1];//设置选项中图片等的左上角的位置  
    17.       
    18.     //获取指定索引选项的图片imageForSegmentAtIndex:  
    19.     UIImageView *imageForSegmentAtIndex = [[UIImageView alloc]initWithImage:[segmentedControl imageForSegmentAtIndex:1]];  
    20.     imageForSegmentAtIndex.frame = CGRectMake(60.0, 100.0, 30.0, 30.0);  
    21.       
    22.     //获取指定索引选项的标题titleForSegmentAtIndex  
    23.     UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(100.0, 100.0, 30.0, 30.0)];  
    24.     titleForSegmentAtIndex.text = [segmentedControl titleForSegmentAtIndex:0];  
    25.       
    26.     //获取总选项数segmentedControl.numberOfSegments  
    27.     UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(140.0, 100.0, 30.0, 30.0)];  
    28.     numberOfSegments.text = [NSString stringWithFormat:@"%d",segmentedControl.numberOfSegments];  
    29.       
    30.     //获取指定索引选项的宽度widthForSegmentAtIndex:  
    31.     UILabel *widthForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(180.0, 100.0, 70.0, 30.0)];  
    32.     widthForSegmentAtIndex.text = [NSString stringWithFormat:@"%f",[segmentedControl widthForSegmentAtIndex:2]];  
    33.       
    34.     segmentedControl.selectedSegmentIndex = 2;//设置默认选择项索引  
    35.     segmentedControl.tintColor = [UIColor redColor];  
    36.     segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;//设置样式  
    37.     segmentedControl.momentary = YES;//设置在点击后是否恢复原样  
    38.       
    39.     [segmentedControl setEnabled:NO forSegmentAtIndex:4];//设置指定索引选项不可选  
    40.     BOOL enableFlag = [segmentedControl isEnabledForSegmentAtIndex:4];//判断指定索引选项是否可选  
    41.     NSLog(@"%d",enableFlag);  
    42.       
    43.     [self.view addSubview:widthForSegmentAtIndex];  
    44.     [self.view addSubview:numberOfSegments];  
    45.     [self.view addSubview:titleForSegmentAtIndex];  
    46.     [self.view addSubview:imageForSegmentAtIndex];  
    47.     [self.view addSubview:segmentedControl];  
    48.       
    49.     [widthForSegmentAtIndex release];  
    50.     [numberOfSegments release];  
    51.     [titleForSegmentAtIndex release];  
    52.     [segmentedTemp release];  
    53.     [imageForSegmentAtIndex release];  
    54.       
    55.     //移除所有选项  
    56.     //[segmentedControl removeAllSegments];  
     
    第三种

    嗯是这样的 我之前也是做的分段控件 其实是有效果的 但是点击很不明显,后来是用了4张图片,2张是作为高亮图片。
    UIImage *segment_hightImage2 = [UIImage imageNamed:NAME_SEGMENTINDEXSECOND_HIGHLIGHTIMAGE];

    [m_catalogControl setImage:segment_hightImage2 forSegmentAtIndex:1];

    UIImage *segment_image4 = [UIImage imageNamed:NAME_SEGMENTINDEXFIRST_IMAGE];//此处设置了分段控件选中第二个item的高亮图片

    [m_catalogControl setImage:segment_image4 forSegmentAtIndex:0];

    注意交错就可以了 点击2位置的时候把其他的再设置为默认图片,被点的设置为高亮图片

     原文:http://blog.csdn.net/ch_soft/article/details/6757171

  • 相关阅读:
    【转】ps墨镜哥教你怎么变成合影帝
    【转】MySQL修改密码方法总结
    【技术贴】五分钟解决打开软件提示windows正在设置Microsoft Office Profes
    【技术贴】IIS相关问题解决笔记。
    【技术贴】五分钟解决打开软件提示windows正在设置Microsoft Office Profes
    【转】小生我怕怕工具包[2010.06.17](转自52破解论坛)
    【技术贴】Realtek HD声卡下QQ语音话筒没声音解决办法
    【技术贴】所有好友的QQ空间都打不开进不去的超简单解决办法!
    ASP.NET IIS 注册工具
    关于服务器如何设置动易系统数据库路径的方法
  • 原文地址:https://www.cnblogs.com/gaohe/p/5070413.html
Copyright © 2020-2023  润新知