iphone ios7 segmented UISegmentedControl change only border color

ort11 picture ort11 · Sep 27, 2013 · Viewed 20.8k times · Source

Been looking around and trying to change just the border color (with a different text color) with no luck. Can change the tint, but changes both the text and border.

Answer

kkodev picture kkodev · Sep 27, 2013

You can use UIAppearance proxy to set title text attributes but preserve tintColor for borders. Something like:

 [[UISegmentedControl appearance] setTitleTextAttributes:@{ 
    NSForegroundColorAttributeName : [UIColor redColor] 
 } forState:UIControlStateNormal];

Edit:

To tint images, you can use something like this in category on UImage:

- (instancetype)tintedImageWithColor:(UIColor *)tintColor {
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGRect rect = (CGRect){ CGPointZero, self.size };
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    [self drawInRect:rect];

    CGContextSetBlendMode(context, kCGBlendModeSourceIn);
    [tintColor setFill];
    CGContextFillRect(context, rect);

    UIImage *image  = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}