Objective c UISegmentedControl不允许我编辑所选颜色

Objective c UISegmentedControl不允许我编辑所选颜色,objective-c,xcode,cocoa-touch,uisegmentedcontrol,Objective C,Xcode,Cocoa Touch,Uisegmentedcontrol,因此,我有一段代码,我希望将分段控制器的选定颜色设置为我要求的颜色,将未选定的分段设置为另一种颜色,请参见以下内容: //normal segment NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Rok" size:20.0],UITextAttributeFont,

因此,我有一段代码,我希望将分段控制器的选定颜色设置为我要求的颜色,将未选定的分段设置为另一种颜色,请参见以下内容:

    //normal segment
NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [UIFont fontWithName:@"Rok" size:20.0],UITextAttributeFont,
                                  [UIColor colorWithRed:75.0/255.0 green:75.0/255.0 blue:75.0/255.0 alpha:1.0], UITextAttributeTextColor,
                                  [UIColor clearColor], UITextAttributeTextShadowColor,
                                  [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
                                  nil];//[NSDictionary dictionaryWithObject:  [UIColor redColor]forKey:UITextAttributeTextColor];
[segmentedControl setTitleTextAttributes:normalAttributes forState:UIControlStateNormal];

NSDictionary *selectedAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [UIFont fontWithName:@"Rok" size:20.0],UITextAttributeFont,
                                    [UIColor whiteColor], UITextAttributeTextColor,
                                    [UIColor clearColor], UITextAttributeTextShadowColor,
                                    [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
                                    nil] ;//[NSDictionary dictionaryWithObject:  [UIColor redColor]forKey:UITextAttributeTextColor];
[segmentedControl setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
那么我做错了什么?直接改变所选片段的颜色是如此困难,这真是令人沮丧!我很想用一排按钮


感谢所有帮助您的人。

在您的UISegmentedControl的值更改事件中添加此代码:

 for (int i=0; i<[sender.subviews count]; i++) 
 {
   if ([[sender.subviews objectAtIndex:i]isSelected] ) 
   {               
     UIColor *tintcolor=[UIColor redColor]; //your requiremnent color here
     [[sender.subviews objectAtIndex:i] setTintColor:tintcolor];
     break;
   }
 }

for(int i=0;i在UISegmentedControl的值更改事件中添加此代码:

 for (int i=0; i<[sender.subviews count]; i++) 
 {
   if ([[sender.subviews objectAtIndex:i]isSelected] ) 
   {               
     UIColor *tintcolor=[UIColor redColor]; //your requiremnent color here
     [[sender.subviews objectAtIndex:i] setTintColor:tintcolor];
     break;
   }
 }

用于(int i=0;ichange UISegmentedControl selected segment colorchange UISegmentedControl selected segment Colors这太棒了,非常感谢。我如何让它在加载时进行颜色调整?如中所述,如果我将该代码放在卸载方法中,它还没有更改选定的索引,因此会进行生物更新-理想情况下,选择上的颜色是这种颜色ed段需要在首次加载时应用。此外,我注意到,当您单击另一个元素时,它会保持红色,您如何将其调整回以前的颜色选择?或者您是否必须将其明确设置为新颜色以还原所做的更改?这太棒了,非常感谢。我如何让它进行此操作加载时的颜色调整?如中所述,如果我将该代码放入卸载方法中,它尚未更改所选索引,因此会进行bio更新-理想情况下,所选片段上的颜色需要在首次加载时应用。此外,我注意到,当您单击另一个元素时,它会保持红色,如何将调整回上一个元素ious颜色选择?或者您是否必须将其明确设置为新颜色以恢复所做的更改?