How can I set the icon image for selected
state of the UISegmentedControl
? Is the UISegmentedControl
really missing this functionality or it is just me missing skills?
Swift 4
This is how I managed it. Make a segmented control and insert segments using the images.
Be sure to set one image as selected as this will be the initial selected index. Also set the backgroundColor and tintColor to clear so that segmented control will only show your images.
var newSegmentedControl: UISegmentedControl = {
let segmentedControl = UISegmentedControl()
segmentedControl.insertSegment(with: UIImage(named: "x_Selected.jpg"), at: 0, animated: true)
segmentedControl.insertSegment(with: UIImage(named: "y_Normal.jpg"), at: 1, animated: true)
segmentedControl.addTarget(self, action: #selector(segmentedControlChanged), for: .valueChanged)
segmentedControl.selectedSegmentIndex = 0
segmentedControl.backgroundColor = .clear
segmentedControl.tintColor = .clear
}()
Function called when the value of segmented control changes.
@objc func segmentedControlChanged(sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
sender.setImage(UIImage(named: "x_Selected.jpg"), forSegmentAt: sender.selectedSegmentIndex)
sender.setImage(UIImage(named: "y_Normal.jpg"), forSegmentAt: 1)
} else if sender.selectedSegmentIndex == 1 {
sender.setImage(UIImage(named: "x_Normal.jpg"), forSegmentAt: 0)
sender.setImage(UIImage(named: "y_Selected.jpg"), forSegmentAt: sender.selectedSegmentIndex)
}
}
Hope you find this useful.. :)
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
-(void)segmentAction:(UISegmentedControl*)sender {
[sender setImage:[UIImage imageNamed:@"image.png"] forSegmentAtIndex:sender.selectedSegmentIndex];
}
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
}
}
[segmentedControl setBackgroundImage:image
forState:UIControlStateSelected
barMetrics:UIBarMetricsDefault]
You also have to set an image for UIControlStateNormal
for this to take effect.
Apple documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With