-1

How to change the tint color of disable segment in UISegmentedControl. I got the solution to sort the segmentedControl.subviews , below is the swift code, please convert it to objective c.

@IBAction func indexChanged(sender: UISegmentedControl) {

let sortedViews = sender.subviews.sort( { $0.frame.origin.x < $1.frame.origin.x } )

for (index, view) in sortedViews.enumerate() {
    if index == sender.selectedSegmentIndex {
        view.tintColor = UIColor.blueColor()
    } else {
        view.tintColor = UIColor.lightGrayColor()
    }
}

}

let sortedViews = segmentedControlOutletVariable.subviews.sort( { $0.frame.origin.x < $1.frame.origin.x } )
sortedViews[0].tintColor = UIColor.blueColor()
rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

2

Check the following code:

1.It assigns green colour to selected segment.(segment-2)

2.Assigns blue colour to those not selected but enabled(segment-1 and 3).

3.Assigns lightGray colour for segments which are disabled.(segment-4)

-(IBAction)segmentedTapped:(UISegmentedControl*)sender{

for(int i=0;i<[sender.subviews count];i++)
{
    if ([[sender.subviews objectAtIndex:i]isEnabled])
    {


        if([[sender.subviews objectAtIndex:i]isSelected])
        {
            UIColor *tintcolor=[UIColor greenColor];
            [[sender.subviews objectAtIndex:i] setTintColor:tintcolor]; //sets green color for selected segment which is enabled
        }
        else
        {
            [[sender.subviews objectAtIndex:i] setTintColor:[UIColor blueColor]]; //sets blue colour for remaining segments which are enabled but not selected.

        }
    }
    else
    {
        [[sender.subviews objectAtIndex:i] setTintColor:[UIColor lightGrayColor]];//sets ight gray for disabled segment.
    }
}
}

The result will be: enter image description here

if you want more information you can get from here:

UISegmentedControl selected segment color

Community
  • 1
  • 1
UdayM
  • 1,725
  • 16
  • 34