码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 之 UISegmentedControl 使用详解

时间:2016-04-29 15:34:15      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:

NSArray *segmentedArray = @[@"1",@"2",@"3",@"4"];  

    //初始化UISegmentedControl  

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

    segmentedControl.frame = CGRectMake(20.0, 20.0250.050.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.0120.030.030.0);  ;

    

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

    UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(100.0160.030.030.0)];  

    titleForSegmentAtIndex.text = [segmentedControl titleForSegmentAtIndex:0];  

    

    //获取总选项数segmentedControl.numberOfSegments  

    UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(140.0170.030.030.0)];  

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

 

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

    UILabel *widthForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(180.0210.070.030.0)];  

    widthForSegmentAtIndex.text = [NSString stringWithFormat:@"%f",[segmentedControl widthForSegmentAtIndex:2]];  

     

   // [segmentedControl setEnabled:NO forSegmentAtIndex:4];//设置指定索引选项不可选  

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

      [mySegmentedControladdTarget:selfaction:@selector(segmentAction:)forControlEvents:UIControlEventValueChanged];  //添加委托方法

//具体委托方法实例   

-(void)segmentAction:(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;

        case 4:

            [self selectmyView5];

            break;

        case 5:

            [self selectmyView6];

            break;    

        default:

            break;

    }

}

 

基本属性、方法如上,大家在实际开发中可根据实际需求选取适当属性和方法。

 

使用技巧:

在导航栏中添加UISegmentedControl ,实现的效果:

实现的代码:

//自定义UISegmentedcontrol

UISegmentedControl *segmentedControl=[[UISegmentedControl alloc] initWithFrame:CGRectMake(80.0f, 8.0f, 200.0f, 30.0f) ]; 
    [segmentedControl insertSegmentWithTitle:@"Food to eat" atIndex:0 animated:YES]; 
    [segmentedControl insertSegmentWithTitle:@"Food to avoid" atIndex:1 animated:YES]; 
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; 
    segmentedControl.momentary = YES; 
    segmentedControl.multipleTouchEnabled=NO; 
    [segmentedControl addTarget:self action:@selector(Selectbutton:) forControlEvents:UIControlEventValueChanged]; 
    UIBarButtonItem *segButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];  //自定义UIBarButtonItem,封装定义好的UIsegmented。
    [segmentedControl release]; 
    self.navigationItem.rightBarButtonItem = segButton;  //添加到导航栏中
    [segButton release];

iOS 之 UISegmentedControl 使用详解

标签:

原文地址:http://blog.csdn.net/mazegong/article/details/51272428

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