iphone ios7 segmented UISegmentedControl change only border color

后端 未结 6 1889
陌清茗
陌清茗 2020-12-29 13:47

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.

6条回答
  •  孤城傲影
    2020-12-29 14:08

    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;
    }
    

提交回复
热议问题