以下は、オン/オフの色を持つカスタム セグメント化されたコントロールのアプリで使用しているコードです。ただし、iOS 6 では、ビューが最初にロードされたときはデフォルトの色です。セグメントの 1 つを選択すると、適切なカスタム カラーが表示されます。ビューの読み込み時に表示し、iOS 6 以外のデバイスとの互換性を確保するには、どうすればよいですか?
#define kTagFirst 111
#define kTagSecond 112
#define kTagThird 113
- (IBAction)segmentedControlChanged:(id)sender
{
UIColor *darkBG = [UIColor colorWithRed:149.0/255.0 green:133.0/255.0 blue:115.0/255.0 alpha:1.0];
UIColor *lightBG = [UIColor colorWithRed:234.0/255.0 green:232.0/255.0 blue:217.0/255.0 alpha:1.0];
UIColor *darkText = [UIColor colorWithRed:85.0/255.0 green:72.0/255.0 blue:60.0/255.0 alpha:1.0];
UIColor *lightText = [UIColor whiteColor];
[segmentedControl setTintColor:darkBG forTag:kTagFirst];
[segmentedControl setTintColor:darkBG forTag:kTagSecond];
[segmentedControl setTintColor:darkBG forTag:kTagThird];
// [segmentedControl setTextColor:lightText forTag:kTagFirst];
// [segmentedControl setTextColor:lightText forTag:kTagSecond];
// [segmentedControl setTextColor:lightText forTag:kTagThird];
switch (segmentedControl.selectedSegmentIndex)
{
case 0:
mapView.hidden = YES;
infoView.hidden = YES;
[segmentedControl setTintColor:darkBG forTag:kTagFirst];
[segmentedControl setTintColor:lightBG forTag:kTagSecond];
[segmentedControl setTintColor:lightBG forTag:kTagThird];
[segmentedControl setTextColor:lightText forTag:kTagFirst];
[segmentedControl setTextColor:darkText forTag:kTagSecond];
[segmentedControl setTextColor:darkText forTag:kTagThird];
//[segmentedControl setShadowColor:lightBG forTag:kTagFirst];
[segmentedControl setShadowColor:lightBG forTag:kTagSecond];
[segmentedControl setShadowColor:lightBG forTag:kTagThird];
break;
case 1:
mapView.hidden = NO;
infoView.hidden = YES;
[segmentedControl setTintColor:lightBG forTag:kTagFirst];
[segmentedControl setTintColor:darkBG forTag:kTagSecond];
[segmentedControl setTintColor:lightBG forTag:kTagThird];
[segmentedControl setTextColor:darkText forTag:kTagFirst];
[segmentedControl setTextColor:lightText forTag:kTagSecond];
[segmentedControl setTextColor:darkText forTag:kTagThird];
[segmentedControl setShadowColor:lightBG forTag:kTagFirst];
//[segmentedControl setShadowColor:lightBG forTag:kTagSecond];
[segmentedControl setShadowColor:lightBG forTag:kTagThird];
break;
case 2:
mapView.hidden = YES;
infoView.hidden = NO;
[segmentedControl setTintColor:lightBG forTag:kTagFirst];
[segmentedControl setTintColor:lightBG forTag:kTagSecond];
[segmentedControl setTintColor:darkBG forTag:kTagThird];
[segmentedControl setTextColor:darkText forTag:kTagFirst];
[segmentedControl setTextColor:darkText forTag:kTagSecond];
[segmentedControl setTextColor:lightText forTag:kTagThird];
[segmentedControl setShadowColor:lightBG forTag:kTagFirst];
[segmentedControl setShadowColor:lightBG forTag:kTagSecond];
//[segmentedControl setShadowColor:lightBG forTag:kTagThird];
break;
}
}
- (void)viewDidLoad
{
[segmentedControl setTag:kTagFirst forSegmentAtIndex:2];
[segmentedControl setTag:kTagSecond forSegmentAtIndex:1];
[segmentedControl setTag:kTagThird forSegmentAtIndex:0];
[self segmentedControlChanged:nil];
mapView.hidden = YES;
infoView.hidden = YES;
segmentedControl.selectedSegmentIndex = 0;