iOS 6分段控制自定义色调

时间:2012-11-23 16:00:47

标签: ios6 uisegmentedcontrol tintcolor

下面是我在我的应用程序中使用的代码,用于带有开/关颜色的自定义分段控件。但是,对于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;

1 个答案:

答案 0 :(得分:0)

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

希望有所帮助