【问题标题】:UISegmentedControl will not allow me to edit the selected colourUISegmentedControl 不允许我编辑选定的颜色
【发布时间】:2012-08-23 16:44:58
【问题描述】:

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

    //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 选定的段颜色

标签: objective-c xcode cocoa-touch uisegmentedcontrol


【解决方案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;
   }
 }

【讨论】:

  • 太棒了,非常感谢。我将如何让它在加载时进行这种颜色调整?如,如果我将该代码放在卸载方法中,它尚未更改所选索引,因此进行了生物更新 - 理想情况下,所选段上的此色调需要在首次加载时应用。此外,我注意到当您单击另一个元素时它会保持红色,您如何调整回之前的颜色选择?还是您必须明确将其设置为新颜色才能恢复所做的更改?
猜你喜欢
  • 2019-04-02
  • 1970-01-01
  • 2010-11-14
  • 2012-12-21
  • 1970-01-01
  • 2022-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多