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
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
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
Swift
To change the color of an SVG using Swift:
badmintonImgView.image = badmintonImgView.image?.withRenderingMode(.alwaysTemplate)
badmintonImgView.tintColor = UIColor.orange
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.
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;
}
}
}
}
For publishing iOS 14.0+, I used אורי orihpt's answer, but realized I had to do some additional steps that differ from other answers on here. When using the Image
, I couldn't get tintColor
to work, but foregroundColor
worked for me. I don't know if you're targeting the whole image, but this solution targets only the SVG paths, instead of creating a background color as well on the image (similar to Marcy's output image).
Image("imageName")
.foregroundColor(Color.red)