Currently I have a small segmented control with 3 separate segments.
What I want to do is, if selected, change the image of that specific segment to a different image.
So far I've managed to make it very similar to what I want, and a new image is displaying when selected, but a small part of the new image is covered by a blue highlight (shown below), and no matter what I try, I can't get rid of it:
For some reason the highlighting is overriding part of the image.
I'd like to know how to completely disable any highlighting/change of a segmented control when selected, or any other option which would achieve my question.
What I've tried so far:
My code (just testing out one image for any selected button as you can see):
-(IBAction)languageChanged:(UISegmentedControl *)sender {
UISegmentedControl *segmentControl = [[UISegmentedControl alloc] init];
[segmentControl addTarget:self action:@selector(segmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentControl];
[sender setImage:[UIImage imageNamed:@"rsz_langue-francais-on.png"] forSegmentAtIndex:sender.selectedSegmentIndex];
}
I am not 100% sure if this will work, but the segment colour is determined by tintColor
.
So you could simply set tintColor
to [UIColor clearColor];
EDIT:
I have read that it is an issue with iOS6 and above. To fix the problem set the width for each individual section rather than the whole segmented control.
Here is some example (untested) code for a UISegmentedControl
with a width of 180:
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 6.0) {
[[UISegmentedControl appearance] setWidth:90 forSegmentAtIndex:0];
[[UISegmentedControl appearance] setWidth:90 forSegmentAtIndex:1];
}
else{
segmentedControl.frame = CGRectMake(0, 0, 180, 30);
}
EDIT 2:
I have only ever been able to change a segment control tint colour when the style is set to 'Bar' rather than 'Plain'. I really up this changes soon as the consistency of colour within my apps is compromised.
A temporary and dirty fix could be to check the momentary state to YES. This would make it only blue for a second and your custom images will still make it seem selected.
In the end, the only way I was able to fix (maybe avoid would be a better word) the problem was by changing the style of the uisegmentedcontrol
from 'Plain' to 'Bar', which removed the blue highlighted spacing in between segments, like Patrick suggested above. I have heard this is a known iOS6 bug, and hopefully it is fixed soon.
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