5

在我的应用程序中,我能够更改所选段控件的颜色。但是颜色会更改为另一个索引而不是所选索引。我可以在索引中找到任何错误。

帮我!

我的代码如下:

if([SegmentRound selectedSegmentIndex] == 0)
    {

        UIColor *newSelectedTintColor2 = [UIColor colorWithRed: 98/255.0 green:156/255.0 blue:247/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:0] setTintColor:newSelectedTintColor2];

        UIColor *newSelectedTintColor1 = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:1] setTintColor:newSelectedTintColor1];

        UIColor *newSelectedTintColor0 = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:2] setTintColor:newSelectedTintColor0];



        FLAGROUND=1;
    }

    if([SegmentRound selectedSegmentIndex] == 1)
    {
        UIColor *newSelectedTintColor1 = [UIColor colorWithRed: 98/255.0 green:156/255.0 blue:247/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:1] setTintColor:newSelectedTintColor1];

        UIColor *newSelectedTintColor0 = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:0] setTintColor:newSelectedTintColor0];

        UIColor *newSelectedTintColor2 = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:2] setTintColor:newSelectedTintColor2];

        FLAGROUND=2;
    }
    if([SegmentRound selectedSegmentIndex] == 2)
    {
        UIColor *newSelectedTintColor0 = [UIColor colorWithRed: 98/255.0 green:156/255.0 blue:247/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:2] setTintColor:newSelectedTintColor0];

        UIColor *newSelectedTintColor2 = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:0] setTintColor:newSelectedTintColor2];

        UIColor *newSelectedTintColor1 = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:1] setTintColor:newSelectedTintColor1];
        FLAGROUND=3;
    }

viewwill出现:

[SegmentRound setSelectedSegmentIndex:0];
4

4 回答 4

9

我建议在您的条件之外创建两种颜色,使您的代码更小一些。然后,您可以使用 foreach 迭代您的细分:

UIColor *selectedColor = [UIColor colorWithRed: 98/255.0 green:156/255.0 blue:247/255.0 alpha:1.0];
UIColor *deselectedColor = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];

for (UIControl *subview in [SegmentRound subviews]) {
    if ([subview isSelected]) 
       [subview setTintColor:selectedColor]; 
    else
       [subview setTintColor:deselectedColor]; 
}
于 2012-08-24T06:37:05.360 回答
2

看看这个

-(IBAction)segmentBtnPressed:(UISegmentedControl*)sender{
for (int i=0; i<[sender.subviews count]; i++) 
{
    if ([[sender.subviews objectAtIndex:i]isSelected] ) 
    {               
    UIColor *tintcolor=[UIColor colorWithRed: 98/255.0 green:156/255.0 blue:247/255.0 alpha:1.0];
    [[sender.subviews objectAtIndex:i] setTintColor:tintcolor];
    }
    else{
     UIColor *tintcolor=[UIColor colorWithRed:127.0/255.0 green:161.0/255.0 blue:183.0/255.0 alpha:1.0];
    [[sender.subviews objectAtIndex:i] setTintColor:tintcolor];

    }
}
}

您也可以在这里查看更多答案UISegmentedControl 选定的段颜色

于 2012-08-24T06:35:30.747 回答
0

我希望您可以简单地更改 Segment Control 的 TintColor。它非常适合我。

于 2016-01-14T19:50:25.737 回答
-1

我尝试在控制台上打印段控制的子视图,我检测到索引顺序相反,这意味着如果 selectedSegment 为 0,那么您的子视图应该是 2 而不是 0。尝试在控制台上打印段控制,您将在段上看到如下相同的结果行动。:

NSArray *theArr = [mSegmentedControl subviews];
            DEBUGLOG(@"controls arr: %@",theArr);

控制台上的日志:

controls arr: (
    "<UISegment: 0x8598ad0; frame = (77 0; 76 34); opaque = NO; layer = <CALayer: 0x8598b30>>",
    "<UISegment: 0x85986e0; frame = (0 0; 76 34); opaque = NO; layer = <CALayer: 0x8598740>>"
)
于 2012-08-24T06:34:28.203 回答