在我的应用程序中,我可以更改所选分段控件的颜色。但是为另一个索引而不是选定的索引更改颜色。我可以在索引中找到任何错误。
帮助我!
我的代码如下:
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;
}
viewWillAppear中:
[SegmentRound setSelectedSegmentIndex:0];
答案 0 :(得分:8)
我建议在您的条件之外创建两种颜色,使您的代码更小一些。然后,您可以使用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];
}
答案 1 :(得分: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];
}
}
}
查看更多答案
答案 2 :(得分:0)
我希望你可以简单地改变段控制的TintColor。它对我来说很完美。
答案 3 :(得分:-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>>"
)