我有一個UISegmentedControl包含3個選項來選擇地圖類型。我將tintcolor設置爲默認的黑色,我想爲選定的段設置綠色。UIsegmentedControl setTintColor奇怪的行爲
這裏是分段控制
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問題,此是綠色色調不適用於正確的部分或有時適用於任何部分。這段代碼有什麼問題?任何幫助將不勝感激。
非常感謝Shubhank。但是這段代碼在iOS-4.x中適用於我,當我在iOS-5設備上運行它時,我遇到了此代碼的問題。對不起,我忘記提到這個問題。在你提到的鏈接中,其他開發人員正在抱怨這一點(適用於iOS <5)。 – chatur 2012-01-18 12:45:40