UISegmentedControl image highlighting bug in iOS6

我只是一个虾纸丫 提交于 2019-12-23 07:13:12

问题


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:

  • setting background image of UISegmentedControl
  • UISegmentedControl custom background image

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];     
}

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/14810493/uisegmentedcontrol-image-highlighting-bug-in-ios6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!