2011-11-04 13 views
5

UISegmentedControlの色を簡単に変更できます。私はthis,this site、そして最高のthis solutionのような様々な解決策を見つけました。しかし、私が望むものはありませんでした。UISegmentedControl with custom color:セパレータラインバグ

これは私のコードだった、私はシンプルなものを作成しようと、それは非常に簡単に動作します。(私は5.0とXcode 4.0.2、iOSの4.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

うわー、これはOption 1Option 2間の問題、(赤い四角でマーク)は、この愚かな青い線私が欲しいものexaclyですので、今すぐOption 3

problem

をクリックします。私は再びOption 1にクリックした場合、私が持っているでしょう:ブルーラインより

boring

が再び表示されます。これは、古いクリックされたセグメント上のすべての左側がこの青い線を持つことを意味します。私が右から左に行くなら、それは起こらない。

これを解決する方法はわかりません。この行にアクセスして色を変更するにはどうすればよいですか?あるいは、私は他のコードを使用する必要があります。多分彼らは同じ問題を抱えているでしょう...

+0

私はあなたと同じ問題を抱えていて、問題を見つけるのに数日を費やしました。あなたが私の人生を救った多くの友達に感謝します.. !!!! – chatur

答えて

3

うわー...私たちが書くのはスタックオーバーフローであり、落ち着くことができます。

If I go from right to left it not happens.

これは解決策である:私は私のOW問題の答えを書きます!私がしなければならないこと?これをシミュレートします。

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