Tint color is not working for image in Xamarin Forms in iOS?

1.2k views Asked by At

I'm working on Embedded Images Tint color in xamarin forms using with this nuget package, working fine in android but not in iOS. As per my search I used rendering mode to AlwaysTemplate but in my scenario I'm unable to use rendering mode. what I'm doing is using the image references from solution file not from iOS assets.

Below is my code snippet.

 <controls:TintedImage
           x:Name="ColorChange"
           Aspect="AspectFit"
           HeightRequest="17"
           Source="{local:ImageResource AppName.Images.dot.png}"
           TintColor="{StaticResource PlaceholderColor}"
           VerticalOptions="Center"
           WidthRequest="17" />

                           

I want to change TintColor if an image without rendering image for iOS in xamarin forms.


1

There are 1 answers

5
nevermore On BEST ANSWER

You can write a custom renderer of Image to change the tint color and UIImageRenderingMode.

In Xamarin.forms:

public class myImage : Image
{

}

In iOS project:

[assembly: ExportRenderer(typeof(myImage), typeof(myImageRenderer))]
namespace WorkingWithImages.iOS
{
    public class myImageRenderer : ImageRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
        {
            base.OnElementChanged(e);

            Control.TintColor = UIColor.Red;

        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (Control.Image != null)
            {
                UIImage image = Control.Image;

                image = image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);

                Control.Image = image;
            }
        }
    }
}