Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 6b6a02e

Browse files
mike-rowleypictos
andauthored
Fix for Bug #934 (IconTintColorEffect does not work when the image is… (#1836)
* Fix for Bug #934 (IconTintColorEffect does not work when the image is in Shared project as EmbeddeResource) * Revised Fix for #934 * Update src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs Co-authored-by: Pedro Jesus <pedrojesus.cefet@gmail.com> * Update src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs Co-authored-by: Pedro Jesus <pedrojesus.cefet@gmail.com> * updated the sample * improved variable name * changed the null check object Co-authored-by: Pedro Jesus <pedrojesus.cefet@gmail.com>
1 parent 6a8c500 commit 6b6a02e

File tree

2 files changed

+60
-20
lines changed

2 files changed

+60
-20
lines changed

samples/XCT.Sample/Pages/Effects/IconTintColorEffectPage.xaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@
3737
HorizontalTextAlignment="Center" />
3838

3939
<Image
40-
Source="https://image.flaticon.com/icons/png/512/48/48639.png"
40+
Source="https://raw.githubusercontent.com/xamarin/XamarinCommunityToolkit/main/samples/XCT.Sample.iOS/Resources/circle.png"
4141
Grid.Row="1" />
4242

4343
<Image
44-
Source="https://image.flaticon.com/icons/png/512/48/48639.png"
44+
Source="https://raw.githubusercontent.com/xamarin/XamarinCommunityToolkit/main/samples/XCT.Sample.iOS/Resources/circle.png"
4545
xct:IconTintColorEffect.TintColor="Red"
4646
Grid.Row="2" />
4747

4848
<Image
49-
Source="https://image.flaticon.com/icons/png/512/48/48639.png"
49+
Source="https://raw.githubusercontent.com/xamarin/XamarinCommunityToolkit/main/samples/XCT.Sample.iOS/Resources/circle.png"
5050
xct:IconTintColorEffect.TintColor="Green"
5151
Grid.Row="3" />
5252

@@ -59,18 +59,18 @@
5959
Grid.Column="1"/>
6060

6161
<ImageButton
62-
Source="https://image.flaticon.com/icons/png/512/48/48639.png"
62+
Source="https://raw.githubusercontent.com/xamarin/XamarinCommunityToolkit/main/samples/XCT.Sample.iOS/Resources/circle.png"
6363
Grid.Row="1"
6464
Grid.Column="1" />
6565

6666
<ImageButton
67-
Source="https://image.flaticon.com/icons/png/512/48/48639.png"
67+
Source="https://raw.githubusercontent.com/xamarin/XamarinCommunityToolkit/main/samples/XCT.Sample.iOS/Resources/circle.png"
6868
xct:IconTintColorEffect.TintColor="Red"
6969
Grid.Row="2"
7070
Grid.Column="1" />
7171

7272
<ImageButton
73-
Source="https://image.flaticon.com/icons/png/512/48/48639.png"
73+
Source="https://raw.githubusercontent.com/xamarin/XamarinCommunityToolkit/main/samples/XCT.Sample.iOS/Resources/circle.png"
7474
xct:IconTintColorEffect.TintColor="Green"
7575
Grid.Row="3"
7676
Grid.Column="1" />

src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.ComponentModel;
1+
using System;
2+
using System.ComponentModel;
23
using UIKit;
34
using Xamarin.CommunityToolkit.Effects;
45
using Xamarin.Forms;
@@ -22,8 +23,8 @@ protected override void OnElementPropertyChanged(PropertyChangedEventArgs args)
2223
base.OnElementPropertyChanged(args);
2324

2425
if (!args.PropertyName.Equals(IconTintColorEffect.TintColorProperty.PropertyName) &&
25-
!args.PropertyName.Equals(Image.SourceProperty.PropertyName) &&
26-
!args.PropertyName.Equals(ImageButton.SourceProperty.PropertyName))
26+
!args.PropertyName.Equals(Image.SourceProperty.PropertyName) &&
27+
!args.PropertyName.Equals(ImageButton.SourceProperty.PropertyName))
2728
return;
2829

2930
ApplyTintColor();
@@ -53,39 +54,78 @@ void ClearTintColor()
5354
{
5455
case UIImageView imageView:
5556
if (imageView.Image != null)
57+
{
58+
Element.PropertyChanged -= ImageViewTintColorPropertyChanged;
5659
imageView.Image = imageView.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
60+
}
61+
5762
break;
5863
case UIButton button:
59-
if (button.CurrentImage != null)
64+
if (button.ImageView.Image != null)
6065
{
66+
Element.PropertyChanged -= ButtonTintColorPropertyChanged;
6167
var originalImage = button.CurrentImage.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
6268
button.SetImage(originalImage, UIControlState.Normal);
6369
}
70+
6471
break;
6572
}
6673
}
6774

6875
void SetUIImageViewTintColor(UIImageView imageView, Color color)
6976
{
7077
if (imageView.Image == null)
71-
return;
78+
{
79+
Element.PropertyChanged += ImageViewTintColorPropertyChanged;
80+
}
81+
else
82+
{
83+
imageView.Image = imageView.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
84+
imageView.TintColor = color.ToUIColor();
85+
}
86+
}
7287

73-
imageView.Image = imageView.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
74-
imageView.TintColor = color.ToUIColor();
88+
void ImageViewTintColorPropertyChanged(object sender, PropertyChangedEventArgs e)
89+
{
90+
if (e.PropertyName == Image.IsLoadingProperty.PropertyName)
91+
{
92+
var element = (Image)Element;
93+
94+
if (!element.IsLoading)
95+
{
96+
ApplyTintColor();
97+
}
98+
}
7599
}
76100

77101
void SetUIButtonTintColor(UIButton button, Color color)
78102
{
79-
if (button.CurrentImage == null)
80-
return;
103+
if (button.ImageView.Image == null)
104+
{
105+
Element.PropertyChanged += ButtonTintColorPropertyChanged;
106+
}
107+
else
108+
{
109+
var templatedImage = button.CurrentImage.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
81110

82-
var templatedImage = button.CurrentImage.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
111+
button.SetImage(null, UIControlState.Normal);
83112

84-
button.SetImage(null, UIControlState.Normal);
113+
button.TintColor = color.ToUIColor();
114+
button.ImageView.TintColor = color.ToUIColor();
115+
button.SetImage(templatedImage, UIControlState.Normal);
116+
}
117+
}
85118

86-
button.TintColor = color.ToUIColor();
87-
button.ImageView.TintColor = color.ToUIColor();
88-
button.SetImage(templatedImage, UIControlState.Normal);
119+
void ButtonTintColorPropertyChanged(object sender, PropertyChangedEventArgs e)
120+
{
121+
if (e.PropertyName == ImageButton.IsLoadingProperty.PropertyName)
122+
{
123+
var element = (ImageButton)Element;
124+
if (!element.IsLoading)
125+
{
126+
ApplyTintColor();
127+
}
128+
}
89129
}
90130
}
91131
}

0 commit comments

Comments
 (0)