UISegmentedControl selected state icon image

后端 未结 6 1673
执笔经年
执笔经年 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条回答
  •  离开以前
    2021-02-20 08:30

    Enhancement of @Kuldeep's Answer

    you may have two or more controls in segmentcontrol

    [_segmentcontrol addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    

    if you want to replace all images differently than use this code..

    -(void)segmentAction:(UISegmentedControl*)sender
    {
        if(sender.selectedSegmentIndex==0)
        {
            [sender setImage:[UIImage imageNamed:@"calf_selected.png"] forSegmentAtIndex:sender.selectedSegmentIndex];//0 index
            [sender setImage:[UIImage imageNamed:@"wrist.png"] forSegmentAtIndex:1];
        }
        else
        {
            [sender setImage:[UIImage imageNamed:@"calf.png"] forSegmentAtIndex:sender.selectedSegmentIndex];//for 1 index
            [sender setImage:[UIImage imageNamed:@"wrist_selected.png"] forSegmentAtIndex:0];
        }
    }
    

提交回复
热议问题