UISegmentedControl选定的段颜色

有什么方法可以自定义选定段的颜色UISegmentedControl

我找到了segmentedController.tintColor属性,该属性使我可以自定义整个分段控件的颜色。问题是,当我为tintColor属性选择鲜艳的颜色时,选定的段几乎变得无法识别(其颜色与分段控件的其余部分几乎相同,因此很难区分选定的段和未选定的段)。因此,我不能使用任何良好的鲜艳颜色进行分段控制。解决方案是为选定的细分颜色提供一些单独的属性,但我找不到它。有人解决了吗?


慕勒3428872
浏览 1324回答 3
3回答

GCT1015

我在UISegmentcontrol中找到了一种为所选段添加颜色的简单方法发件人是UISegmentControlfor (int i=0; i<[sender.subviews count]; i++)&nbsp;{&nbsp; &nbsp; if ([[sender.subviews objectAtIndex:i]isSelected] )&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; UIColor *tintcolor=[UIColor colorWithRed:127.0/255.0 green:161.0/255.0 blue:183.0/255.0 alpha:1.0];&nbsp; &nbsp; [[sender.subviews objectAtIndex:i] setTintColor:tintcolor];&nbsp; &nbsp; }&nbsp; &nbsp;else&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [[sender.subviews objectAtIndex:i] setTintColor:nil];&nbsp; &nbsp; }}检查它为我工作

慕盖茨4494581

这是将所选段更改为任何RGB颜色的绝对最简单的方法。无需子类化或黑客入侵。segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;UIColor *newTintColor = [UIColor colorWithRed: 251/255.0 green:175/255.0 blue:93/255.0 alpha:1.0];&nbsp; &nbsp; segmentedControl.tintColor = newTintColor;UIColor *newSelectedTintColor = [UIColor colorWithRed: 0/255.0 green:175/255.0 blue:0/255.0 alpha:1.0];[[[segmentedControl subviews] objectAtIndex:0] setTintColor:newSelectedTintColor];此示例显示了重要步骤:将控件样式设置为“ StyleBar”,这是工作所需的样式首先将整个控件的未选择颜色设置为橙色将所选线段的颜色设置为绿色笔记:步骤1和2可以在界面生成器中完成,也可以在所示的代码中完成。但是,第3步只能在代码中完成像这样用“ 123.0 / 255.0”这样的符号设置颜色值只是使RGB值脱颖而出的一种方法,而不是UIColor要求的标准化浮点值(如果愿意,可以忽略它)
打开App,查看更多内容
随时随地看视频慕课网APP