本文介绍了UIsegmentedControl setTintColor奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UISegmentedControl,其中包含3个用于选择地图类型的选项.我将tintcolor设置为默认的黑色,并且我想为选定的段设置绿色.

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.

这是分段控件的声明

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

这是选择器方法

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


       }

此代码在设备上效果很好< iOS-5,但在> = iOS-5上,此问题是绿色色调无法应用于正确的细分,有时甚至不适用于任何细分.此代码有什么问题?任何帮助将不胜感激.

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.

推荐答案

感谢这个问题.

如前所述,选择索引不是分配tintcolor的可靠方法.我必须存储他们的ID,并使用它们来分配tintcolor.首先存储ID

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

然后使用它

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

这篇关于UIsegmentedControl setTintColor奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 15:47