xcode 更改所选段控件的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12103961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the color of selected segment control
提问by piyush
In my app,i able to change the color of the selected segment control.But the color is changed for another index rather than selected index. I can find any mistake in the index.
在我的应用程序中,我能够更改所选段控件的颜色。但是更改了另一个索引而不是所选索引的颜色。我可以在索引中找到任何错误。
Help me!
帮我!
my code is as follow:
我的代码如下:
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:
viewwill出现:
[SegmentRound setSelectedSegmentIndex:0];
回答by Herm
I'd recommend to create the two colors outside of your condition, makes your code a bit smaller. Then you can use a foreach to iterate over your segments :
我建议在您的条件之外创建两种颜色,使您的代码更小。然后你可以使用 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];
}
回答by Hiren
check out this one
看看这个
-(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];
}
}
}
Also you can check out more answers here UISegmentedControl selected segment color
您也可以在此处查看更多答案UISegmentedControl selected segment color
回答by Castor
I hope you can simply change the TintColor of Segment Control. It works perfectly for me.
我希望您可以简单地更改 Segment Control 的 TintColor。它非常适合我。
回答by Nuzhat Zari
I tried printing subviews of segment control on console and I detected that indexes are in reverse order, means if selectedSegment is 0 then your subview should be 2 not 0.Try printing segment control on console and you will see the same result as follows on segment action.:
我尝试在控制台上打印段控制的子视图,我检测到索引的顺序相反,这意味着如果 selectedSegment 为 0 那么你的子视图应该是 2 而不是 0。尝试在控制台上打印段控制,你会在段上看到如下相同的结果行动。:
NSArray *theArr = [mSegmentedControl subviews];
DEBUGLOG(@"controls arr: %@",theArr);
Logs on console:
在控制台上登录:
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>>"
)