2

下面是我在我的应用程序中使用的代码,用于具有开/关颜色的自定义分段控件。然而,在 iOS 6 中,当视图首次加载时,它是默认颜色。一旦您选择了其中一个部分,就会出现正确的自定义颜色。我需要做什么才能在视图加载时显示它并确保它与不在 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;
4

1 回答 1

0

您需要将一些逻辑移动到视图中加载方法,该方法在加载时为您的分段栏设置默认色调颜色。现在,您在 IBAction 中拥有所有这些逻辑 - 在第一次选择它之前可能不会被调用。

希望有帮助

于 2012-11-23T16:29:21.963 回答