1

所以我有一些代码,我希望将分段控制器的选定颜色设置为我要求的颜色,并将未选定的段设置为另一种颜色,见下文:

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

那么我做错了什么?直接改变所选段的颜色真是令人沮丧!我很想只使用一排按钮!

感谢任何提供帮助的人。

4

1 回答 1

1

在 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;
   }
 }
于 2012-08-23T08:44:58.367 回答