2011-11-04 50 views
5

它很容易改变UISegmentedControl的颜色。我发现了各种解决方案,如this,this site和最好的this solution。但没有一个是我想要的。UISegmentedControl自定义颜色:分隔线错误

我试图创建一个简单的事情,它的工作很容易,这是我的代码:(我使用的是iOS 4.2的,不是5.0的Xcode 4.0.2)

id segment[3]; 
UISegmentedControl *segmentedControl; 
- (id)init 
{ 
    NSArray *itens = [NSArray arrayWithObjects: @"Option 1", @"Option 2", @"Option 3", nil];  
    segmentedControl = [[UISegmentedControl alloc] initWithItems:itens]; 
    [segmentedControl setFrame:CGRectMake(0, 0, 500, 30)]; 
    [segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar]; 
    [segmentedControl addTarget:self 
        action:@selector(segmentedControl:) 
      forControlEvents:UIControlEventAllEvents]; 

    switch (type) { 
     case type1: [segmentedControl setSelectedSegmentIndex:0]; break; 
     case type2: [segmentedControl setSelectedSegmentIndex:1]; break;   
     case type3: [segmentedControl setSelectedSegmentIndex:2]; break;   
    } 
    for (int i=0; i<3; i++) { 
     //The most important trick to work, have to retain the subviews 
     segment[i] = [[[segmentedControl subviews] objectAtIndex:i] retain]; 
    } 
    [self changeColor]; 
    [self addSubview:segmentedControl]; 
    return self; 
} 

- (void)segmentedControl:(id)sender 
{ 
    //do some thing 
    [self changeColor]; 
} 

- (void)changeColor{ 
    for (int i=0; i<3; i++) { 
     [segment[i] setTintColor:[UIColor lightGrayColor]]; 
    } 
    int select = segmentedControl.selectedSegmentIndex; 
    [segment[select] setTintColor:[UIColor blueColor]];  
} 

因此,创建此:

first image

很好,然后我点击Option 2

second

哇,这是exacly我想要的东西,在Option 3

problem

现在,所以点击这个问题,Option 1Option 2之间的这个愚蠢的蓝线(标记为红色方块)。如果我在Option 1再次点击,我将有:

boring

比蓝线再次出现。这意味着旧点击段(但不是第一个点)上的每个左侧都会有这条蓝线。如果我从右向左走,它不会发生。

我不知道如何解决这个问题。我如何访问这一行并更改颜色?或者我将不得不使用其他代码。也许他们会有同样的问题...

+0

我和你有同样的问题,并花了几天的时间来找到问题。感谢很多朋友你拯救了我的生命.. !!!! – chatur

回答

3

哇...当我们写在stackoverflow,我们可以冷静下来,思考更好。我写的答案在我的流量问题:

If I go from right to left it not happens.

这是解决方案!我必须做什么?模拟这个。

BOOL inside = FALSE; 
- (void)changeColor{ 
    int old = segmentedControl.selectedSegmentIndex; 
    for (int i=0; i<3; i++) { 
     [segment[i] setTintColor:[UIColor lightGrayColor]]; 
    } 
/**/inside = TRUE; 
/**/for (int i=3; i>0; i--) { 
/**/  //When I do this, it call "segmentedControl:" 
/**/ [segmentedControl setSelectedSegmentIndex:i]; 
/**/} 

/**/int select = old; 
    [segment[select] setTintColor:[UIColor blueColor]];  
    [segmentedControl setSelectedSegmentIndex:select];  
/**/inside = FALSE;  
} 

- (void)segmentedControl:(id)sender 
{ 
/**/if (inside==TRUE) return; //Ignore this calls. 
    //do some thing 
    [self changeColor]; 
} 
5

我觉得有一个更简单的解决方案。 只是干净的指针..

for (int i=0; i<[self.segmentedControll.subviews count]; i++) 
{ 
    [[self.segmentedControll.subviews objectAtIndex:i] setTintColor:nil]; 
    if (![[self.segmentedControll.subviews objectAtIndex:i]isSelected]) 
    { 
     UIColor *tintcolor=[UIColor blackColor]; 
     [[self.segmentedControll.subviews objectAtIndex:i] setTintColor:tintcolor]; 
    } 
    else 
    { 
     UIColor *tintcolor=[UIColor blueColor]; 
     [[self.segmentedControll.subviews objectAtIndex:i] setTintColor:tintcolor]; 
    } 
} 
+0

您还必须在viewDidApear中设置颜色,以便在加载时使分段控件显示预期的颜色。好和简单的解决方案。 – mircaea