9

I want to change color of svg images. I am using following code, it hides the image and fill the whole image view with the color set.

 self.badmintonImgView.tintColor = [UIColor orangeColor];

I need to change the color of image. Please suggest

user
  • 131
  • 1
  • 10

5 Answers5

26

If the SVG is in Assets.xcassets, you can tell Xcode to treat the image as an icon:

First, select your SVG image in the assets catalog

Then set the render mode to Template Image

אורי orihpt
  • 1,912
  • 1
  • 12
  • 35
15

Swift

To change the color of an SVG using Swift:

badmintonImgView.image = badmintonImgView.image?.withRenderingMode(.alwaysTemplate)
badmintonImgView.tintColor = UIColor.orange

enter image description here

Marcy
  • 2,834
  • 2
  • 26
  • 39
1

This worked for me:

UIImage * imageOrange = [[UIImage imageNamed:@"yourSVGImageName"] imageWithTintColor:UIColor.orangeColor];
UIImageView * yourImageView = [[UIImageView alloc] initWithImage: imageOrange];

remember add the image to the assets folder. I hope it helps you.

Pedro Trujillo
  • 1,299
  • 16
  • 17
0

Do as follow may be work for you.

SVGKImage *svgImage = [SVGKImage imageNamed:@"YOURIMAGENAME"];
SVGKLayeredImageView *svgImageView = [[SVGKLayeredImageView alloc] initWithSVGKImage:svgImage];
[capturedImageView addSubview:svgImageView];

CALayer* layer = svgImageView.layer;
for (CALayer *subLayer in layer.sublayers) {

    for (CALayer *subSubLayer in subLayer.sublayers) {


        for (CALayer *subSubSubLayer in subSubLayer.sublayers) {

            if( [subSubSubLayer isKindOfClass:[CAShapeLayer class]]){
                CAShapeLayer* shapeLayer = (CAShapeLayer*)subSubSubLayer;
                shapeLayer.fillColor = [UIColor BlackColor].CGColor;
            }
        }
    }
}
Hitesh Surani
  • 11,344
  • 5
  • 46
  • 60
0

Try this [self.badmintonImgView setTintColor:[UIColor redColor]];

Vishal Sonawane
  • 2,537
  • 2
  • 13
  • 21