UISegmentedControl selected state icon image

后端 未结 6 1690
执笔经年
执笔经年 2021-02-20 07:54

How can I set the icon image for selected state of the UISegmentedControl. Is the UISegmentedControl really missing this functionality or

6条回答
  •  Happy的楠姐
    2021-02-20 08:46

    Here I created two UISegmentedControl elements programmatically u can set image icon.

    UISegmentedControl *switchView=[[UISegmentedControl alloc] initWithItems:[[[NSMutableArray alloc] initWithObjects:@"Yes",@"No",nil] autorelease]];
    [switchView setFrame:CGRectMake(180,802,94,27)];
    switchView.selectedSegmentIndex=1;
    switchView.segmentedControlStyle=UISegmentedControlStyleBar;
    [switchView setImage:[UIImage imageNamed:@"switchBlueBg.png"] forSegmentAtIndex:0];   // set icon for when selected
    [switchView setImage:[UIImage imageNamed:@"switchOffPlain.png"] forSegmentAtIndex:1];  //set icon for when unselected
    [switchView addTarget:self action:@selector(checkOnOffState:) forControlEvents:UIControlEventValueChanged];             
    [scrollview addSubview:switchView];
    
    UISegmentedControl *switchView1=[[UISegmentedControl alloc] initWithItems:[[[NSMutableArray alloc] initWithObjects:@"Yes",@"No",nil] autorelease]];
    [switchView1 setFrame:CGRectMake(180,1003,94,27)];
    switchView1.selectedSegmentIndex=1;
    switchView1.segmentedControlStyle=UISegmentedControlStyleBar;
    [switchView setImage:[UIImage imageNamed:@"switchBlueBg.png"] forSegmentAtIndex:0];  // set icon for when selected
    [switchView setImage:[UIImage imageNamed:@"switchOffPlain.png"] forSegmentAtIndex:1];  ////set icon for when unselected
    [switchView1 addTarget:self action:@selector(checkOnOffState1:) forControlEvents:UIControlEventValueChanged];
    [scrollview addSubview:switchView1];
    
    
    
    -(IBAction)checkOnOffState:(id)sender
    {
        UISegmentedControl* tempSeg=(UISegmentedControl *)sender;
    
        if(tempSeg.selectedSegmentIndex==0)
        {
            //your codes
        }
        else
        {
            //your codes
        }   
    }
    

提交回复
热议问题