Iphone 还原取消选择的分段控件的颜色。 类似的内容将在值更改函数中起作用: for (int i=0; i<[control.subviews count]; i++) { if ([[control.subviews objectAtIndex:i]isSelected] ) { UIColor *tintcolor=[UIColor colorWithRed:127.0/255.0 green:161.0/255.0 blue:183.0/255.0 alpha:1.0]; [[control.subviews objectAtIndex:i] setTintColor:tintcolor]; } else { UIColor *tintcolor=[UIColor grayColor]; // default color [[control.subviews objectAtIndex:i] setTintColor:tintcolor]; } } for(int i=0;i

Iphone 还原取消选择的分段控件的颜色。 类似的内容将在值更改函数中起作用: for (int i=0; i<[control.subviews count]; i++) { if ([[control.subviews objectAtIndex:i]isSelected] ) { UIColor *tintcolor=[UIColor colorWithRed:127.0/255.0 green:161.0/255.0 blue:183.0/255.0 alpha:1.0]; [[control.subviews objectAtIndex:i] setTintColor:tintcolor]; } else { UIColor *tintcolor=[UIColor grayColor]; // default color [[control.subviews objectAtIndex:i] setTintColor:tintcolor]; } } for(int i=0;i,iphone,colors,uisegmentedcontrol,Iphone,Colors,Uisegmentedcontrol,在段间切换时,前两个解决方案对我不起作用 我的解决方案是在我的视图控制器中处理段更改事件,然后在每次更改段时调用此方法: + (void)setSegmentedControl:(UISegmentedControl *)segmentedControl selectedColor:(UIColor *)selectedColor deselectedColor:(UIColor *)deselectedColor { for (i

在段间切换时,前两个解决方案对我不起作用

我的解决方案是在我的视图控制器中处理段更改事件,然后在每次更改段时调用此方法:

+ (void)setSegmentedControl:(UISegmentedControl *)segmentedControl 
              selectedColor:(UIColor *)selectedColor 
            deselectedColor:(UIColor *)deselectedColor
{
    for (int i = 0; i < segmentedControl.subviews.count; i++) 
    {
        id subView = [segmentedControl.subviews objectAtIndex:i];

        if ([subView isSelected])
            [subView setTintColor:selectedColor];
        else
            [subView setTintColor:deselectedColor];
    }    
}
+(void)setSegmentedControl:(UISegmentedControl*)segmentedControl
selectedColor:(UIColor*)selectedColor
取消选择颜色:(UIColor*)取消选择颜色
{
对于(int i=0;i
我发现上面的答案非常有用。我正在使用分段控制来设置旋钮的精度。我混合了上面的答案,得出了以下结论:

-(void) viewDidLoad {

NSArray *segments = [NSArray arrayWithObjects:@"Course", @"Fine",nil];

[knob setPrecision:0.1]; // initial precision
// Set starting values

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segments];

segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(120, 680, 228, 30);
[segmentedControl addTarget:self action:@selector(precisionSelect:) forControlEvents:UIControlEventValueChanged];
segmentedControl.momentary = YES;

[self.view addSubview:segmentedControl];
}   

- (void)precisionSelect:(UISegmentedControl*)sender
{   
    UIColor *tintcolor = [UIColor darkGrayColor];   
    if (sender.selectedSegmentIndex == 0) {
        [[sender.subviews objectAtIndex:0] setTintColor:nil];
        [[sender.subviews objectAtIndex:1] setTintColor:tintcolor];
    [knob setPrecision:0.1]; // Coarse
    } else {
        [[sender.subviews objectAtIndex:0] setTintColor:tintcolor];
        [[sender.subviews objectAtIndex:1] setTintColor:nil];
    [knob setPrecision:0.05]; // Fine
    }

}
希望这能帮助别人。。
对我来说,关键是能够使用以下命令重置未选择的索引:
setTintColor:nil];

要做到这一点,您只需找到所选的段,例如,通过迭代分段控件的子视图并测试
isSelected
属性,然后简单地调用该子视图上的
setTintColor:
方法

我通过在Interface Builder中的ValueChanged事件中将一个操作连接到每个分段控件来实现这一点,我将它们连接到视图控制器文件中的此方法,该文件本质上是msprague的答案:

- (IBAction)segmentedControlValueChanged:(UISegmentedControl*)sender
{
    for (int i=0; i<[sender.subviews count]; i++)
    {
        if ([[sender.subviews objectAtIndex:i] respondsToSelector:@selector(isSelected)] && [[sender.subviews objectAtIndex:i]isSelected])
        {
            [[sender.subviews objectAtIndex:i] setTintColor:[UIColor whiteColor]];
        }
        if ([[sender.subviews objectAtIndex:i] respondsToSelector:@selector(isSelected)] && ![[sender.subviews objectAtIndex:i] isSelected])
        {
            [[sender.subviews objectAtIndex:i] setTintColor:[UIColor blackColor]];
        }
    }
}
对于某些附加点,如果您确实希望将分段控件设置为在选择时使用白色,那么您还希望在选择文本时将文本的颜色更改为黑色,您可以这样做:

//Create a dictionary to hold the new text attributes
NSMutableDictionary * textAttributes = [[NSMutableDictionary alloc] init];
//Add an entry to set the text to black
[textAttributes setObject:[UIColor blackColor] forKey:UITextAttributeTextColor];
//Set the attributes on the desired control but only for the selected state
[segmentedControlOne setTitleTextAttributes:textAttributes forState:UIControlStateSelected];
随着iOS 6的引入,首次在ViewDidAspect方法中设置所选项目的着色颜色将不起作用,为了解决这个问题,我使用grand central dispatch在几秒钟后更改所选颜色,如下所示:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.05 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [self segmentedControlValueChanged:segmentedControlOne];
    });

为了澄清@jothikenpachi提供的上述答案,我们发现以下UISegmentController类别在iOS6中运行良好,并允许在段上使用任意的开/关颜色方案。此外,如果在将来的操作系统版本中更改了私有方法isSelected/setTintColor:,则它将优雅地失败。关于私有API调用的警告,等等

@implementation UISegmentedControl(CustomTintExtension) {
-(void) updateCustomTintColorOn:(UIColor*)onColor Off:(UIColor*)offColor {
// Convenience function to rest the tint colors after selection, called upon change of selected index

SEL tint = @selector(setTintColor:);

for (UIView *view in [self subviews]) {
    // Loop through the views...
    if (view && ([view respondsToSelector:tint])) {
        [view performSelector:tint withObject:nil];
    }
    if (view && ([view respondsToSelector:tint])) {
        [view performSelector:tint withObject:offColor];
    }
}

// Checking if segment subview is selected...
SEL isSelected = @selector(isSelected);
for (UIView *view in [self subviews]) {
    if ([view respondsToSelector:isSelected] && [view performSelector:isSelected withObject:nil])
    {
        [view performSelector:tint withObject:onColor];
        break;
    }
}

}
注意,此category方法将从UISegmentController的
-(iAction)segmentAction:(id)sender
方法中调用


还请注意,对于iOS6,您可能需要首先在管理UIViewController的
-(void)viewdidebeen:(BOOL)animated
中调用此方法,这可能会导致动画闪烁。要最小化此情况,请尝试设置“offColor”作为iSegmentController在IB中的tintColor.

我在iOS 7上遇到了这个问题,它的工作方式与iOS 6不同

在iOS 7中,所选段的标签颜色与UISegmentControl背景颜色相同。在iOS 7中更改它的唯一方法是设置UISegmentControl的背景颜色

segmentControl.backgroundColor = customColor;
使用以下命令:

[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:255.0/255 green:37.0/255 blue:99.0/255 alpha:1.0]} forState:UIControlStateSelected];

我用了这个,它一步就改变了所有的颜色

mySegmentedControl.tintColor = [UIColor redColor]

-(iAction)segmentedControlValueChanged:(UISegmentedControl*)发送方{
对于(int i=0;i
我想知道为什么有人没有提到UIAppearanceProxy

苹果文档::

    private class func applyUISegmentControlAppearance(){
    let apperance = UISegmentedControl.appearance()

    // Set Navigation bar Title colour
    let unselAttrib = [NSForegroundColorAttributeName:UIColor.yellow,
                                        NSFontAttributeName: UIFont.systemFont(ofSize: 15)]

    let selAttrib = [NSForegroundColorAttributeName:UIColor.red,
                     NSFontAttributeName: UIFont.boldSystemFont(ofSize: 15)]


    apperance.setTitleTextAttributes(unselAttrib, for: .normal)
    apperance.setTitleTextAttributes(selAttrib, for: .selected)
}

示例代码:

    private class func applyUISegmentControlAppearance(){
    let apperance = UISegmentedControl.appearance()

    // Set Navigation bar Title colour
    let unselAttrib = [NSForegroundColorAttributeName:UIColor.yellow,
                                        NSFontAttributeName: UIFont.systemFont(ofSize: 15)]

    let selAttrib = [NSForegroundColorAttributeName:UIColor.red,
                     NSFontAttributeName: UIFont.boldSystemFont(ofSize: 15)]


    apperance.setTitleTextAttributes(unselAttrib, for: .normal)
    apperance.setTitleTextAttributes(selAttrib, for: .selected)
}
呼叫来源: 您可以在中的
AppDelegate
中调用此方法

application(应用程序:UIApplication,willFinishLaunchingWithOptions启动选项:[uiapplicationaunchoptions:Any]?=nil)->Bool


这个Swift 4代码适合我

segmentedControl.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for: .selected)

我知道这是一个老问题,但现在在xcode 11+中,您可以设置所选的段着色颜色


在代码中,我们可以使用
selectedSegmentTintColor
。iOS 13+

是的,谢谢,如果你想要定制颜色,猜测使用看起来像分段控件的UIButtons是唯一的方法…@Mike:我也这么认为。也许有人会建议一些其他解决方案,而不是黑客和使用文档化的api。疯狂的道具给你,伙计发布这个老问题的答案。我发现了,你的回答帮了我很大的忙!我已经实现了一个分段控件,所以我只需要使用最后一行。效果非常好,谢谢!@Lee Whitney,我认为所选分段的索引与分段控件子视图的索引不同。这使得太有意义了。我只是不能让它点击。我在IB中创建了一个2段的UISegCont,选择了索引0,并将色调分配给了darkgray。然后我使用最后一行定义所有选定段的颜色。但是当VC出现时,索引0段为黑色,索引1段为darkgray。然后当我点击索引1段时,索引0段确实出现在我的自定义颜色中。嗯?似乎每次UI段状态更改时都需要定义颜色。这不起作用。将NewIntColor设置为整个控件的tintColor正在起作用,但将NewSelectedIntColor设置为第一个子视图的tintColor没有任何作用。注意:。segmentedControlStyle wa在iOS 7.0中已弃用。如果子视图响应选择器isSelected和setTintColor,或者当Apple更改UISegmentedControl的工作方式时,应添加健全性检查
//Create a dictionary to hold the new text attributes
NSMutableDictionary * textAttributes = [[NSMutableDictionary alloc] init];
//Add an entry to set the text to black
[textAttributes setObject:[UIColor blackColor] forKey:UITextAttributeTextColor];
//Set the attributes on the desired control but only for the selected state
[segmentedControlOne setTitleTextAttributes:textAttributes forState:UIControlStateSelected];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.05 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [self segmentedControlValueChanged:segmentedControlOne];
    });
@implementation UISegmentedControl(CustomTintExtension) {
-(void) updateCustomTintColorOn:(UIColor*)onColor Off:(UIColor*)offColor {
// Convenience function to rest the tint colors after selection, called upon change of selected index

SEL tint = @selector(setTintColor:);

for (UIView *view in [self subviews]) {
    // Loop through the views...
    if (view && ([view respondsToSelector:tint])) {
        [view performSelector:tint withObject:nil];
    }
    if (view && ([view respondsToSelector:tint])) {
        [view performSelector:tint withObject:offColor];
    }
}

// Checking if segment subview is selected...
SEL isSelected = @selector(isSelected);
for (UIView *view in [self subviews]) {
    if ([view respondsToSelector:isSelected] && [view performSelector:isSelected withObject:nil])
    {
        [view performSelector:tint withObject:onColor];
        break;
    }
}

}
segmentControl.backgroundColor = customColor;
[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:255.0/255 green:37.0/255 blue:99.0/255 alpha:1.0]} forState:UIControlStateSelected];
mySegmentedControl.tintColor = [UIColor redColor]
- (IBAction)segmentControlValueChanged:(UISegmentedControl *)sender
{
    if ([[sender.subviews firstObject] respondsToSelector:@selector(setTintColor:)]) {
        for (id segment in sender.subviews) {
            if ([segment respondsToSelector:@selector(isSelected)] && [segment isSelected]) {
                [segment setTintColor:[UIColor redColor]];
            } else {
                [segment setTintColor:[UIColor grayColor]];
            }
        }
    }
}
Try this solution.    
        @IBAction func dashBoardSegmentValueChanged(sender: AnyObject) {
            switch dashBoardSegment.selectedSegmentIndex
            {
            case 0:     
                sender.subviews.last?.backgroundColor = UIColor.whiteColor()
                sender.subviews.first?.backgroundColor =  UIColor.clearColor()

                break;
            case 1:            
                sender.subviews.first?.backgroundColor =  UIColor.whiteColor()
                sender.subviews.last?.backgroundColor = UIColor.clearColor()
                break;
            default:
                break;
            }
        }

Note: Make sure you select one segment subview as initial selected for easiness. It works if you have two segment subviews.
- (IBAction)segmentedControlValueChanged:(UISegmentedControl *)sender {
    for (int i = 0; i < sender.subviews.count; i++) {
        UIControl *component = [sender.subviews objectAtIndex:i];
        if ([component respondsToSelector:@selector(isSelected)]) {
            UIColor *selectedColor = [UIColor greenColor];
            UIColor *normalColor   = [UIColor blackColor];
            UIColor *tint = component.isSelected ? selectedColor : normalColor;
            [component setTintColor:tint];
        }
    }
}
    private class func applyUISegmentControlAppearance(){
    let apperance = UISegmentedControl.appearance()

    // Set Navigation bar Title colour
    let unselAttrib = [NSForegroundColorAttributeName:UIColor.yellow,
                                        NSFontAttributeName: UIFont.systemFont(ofSize: 15)]

    let selAttrib = [NSForegroundColorAttributeName:UIColor.red,
                     NSFontAttributeName: UIFont.boldSystemFont(ofSize: 15)]


    apperance.setTitleTextAttributes(unselAttrib, for: .normal)
    apperance.setTitleTextAttributes(selAttrib, for: .selected)
}
segmentedControl.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for: .selected)
[segmentedControl setSelectedSegmentTintColor:[UIColor darkGrayColor]];

//For iOS 13