gpt4 book ai didi

iphone - 更改选定段控件的颜色

转载 作者:可可西里 更新时间:2023-11-01 03:44:11 26 4
gpt4 key购买 nike

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

帮帮我!

我的代码如下:

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];

最佳答案

我建议在您的条件之外创建两种颜色,使您的代码更小一些。然后您可以使用 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];
}

关于iphone - 更改选定段控件的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12103961/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com