UIsegmentedControl setTintColor strange behaviour

南楼画角 提交于 2020-01-06 04:33:15

问题


I have a UISegmentedControl containing 3 options for selecting map type. I am setting tintcolor as black as default and I want to set green color for selected segment.

Here is declaration of segmented control

NSArray *mapType = [NSArray arrayWithObjects: @"Map", @"Hyb", @"Sat", nil];
    segmentedMapType = [[UISegmentedControl alloc] initWithItems:mapType] ;
    segmentedMapType.segmentedControlStyle =   UISegmentedControlStyleBar ;
    segmentedMapType.tintColor = [UIColor blackColor];
    segmentedMapType.frame = CGRectMake(100, 40, 120, 40);
   // segmentedMapType.momentary = YES;
    [[[segmentedMapType subviews]objectAtIndex:0] setTintColor:[UIColor colorWithRed:0 green:.6 blue:0 alpha:1]];
    [segmentedMapType setSelectedSegmentIndex:0];

[segmentedMapType addTarget:self action:@selector(selectMap:) forControlEvents:UIControlEventValueChanged];
[mapSettingsView addSubview:segmentedMapType];

And here is the selector method

-(void)selectMap:(id)sender
    {
        int index = [segmentedMapType selectedSegmentIndex];

        UIColor *newSelectedTintColor = [UIColor colorWithRed: 0 green:.6 blue:0 alpha:1.0];

        if (index==0) {
    [[[segmentedMapType subviews] objectAtIndex:2] setTintColor:newSelectedTintColor];
    [[[segmentedMapType subviews] objectAtIndex:1] setTintColor:[UIColor blackColor]];
    [[[segmentedMapType subviews] objectAtIndex:0] setTintColor:[UIColor blackColor]];
} else if (index==1){
    [[[segmentedMapType subviews] objectAtIndex:1] setTintColor:newSelectedTintColor];
    [[[segmentedMapType subviews] objectAtIndex:0] setTintColor:[UIColor blackColor]];
    [[[segmentedMapType subviews] objectAtIndex:2] setTintColor:[UIColor blackColor]];
}
else

if (index==2)
{
    [[[segmentedMapType subviews] objectAtIndex:0] setTintColor:newSelectedTintColor];
    [[[segmentedMapType subviews] objectAtIndex:1] setTintColor:[UIColor blackColor]];
    [[[segmentedMapType subviews] objectAtIndex:2] setTintColor:[UIColor blackColor]];
}


       }

This code works good on devices < iOS-5 but on >=iOS-5 problem with this is green tint color does not get applied to correct segment or sometimes to any segment. What is wrong with this code? Any help will be greatly appreciated.


回答1:


Thanks to this question.

As mentioned there selected index is not a reliable way to assign tintcolor. I had to store their id and use them to assign tintcolor. First store the IDs

for (int i=0; i<3; i++) {
        //The most important trick to work, have to retain the subviews
        segment[i] = [[[segmentedMapType subviews] objectAtIndex:i] retain];
    }

And then use it

-(void)selectMap:(id)sender
{
    int index = [segmentedMapType selectedSegmentIndex];
    UIColor *newSelectedTintColor = [UIColor colorWithRed: 0 green:.7 blue:0 alpha:1.0];
    for (int i=0; i<3; i++) {
        [segment[i] setTintColor:[UIColor blackColor]];
    }
    int select = segmentedMapType.selectedSegmentIndex;
    [segment[select] setTintColor:newSelectedTintColor];  
}



回答2:


for some reason if it's done in viewDidLoad (or anywhere before viewdDidAppear) changes to tint color of single segments (maybe even the whole segmented control, not sure) are not applied until you touch the control.




回答3:


From what i know there is no default way to tint specific segment index tint color..You can only provide tint to whole segment as a whole.

Try this

http://www.framewreck.net/2010/07/custom-tintcolor-for-each-segment-of.html



来源:https://stackoverflow.com/questions/8910074/uisegmentedcontrol-settintcolor-strange-behaviour

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