Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<Grid
Padding="20"
ColumnDefinitions="*, *, *"
RowDefinitions="Auto, Auto, 60, Auto, 60, Auto, Auto, 60"
RowDefinitions="Auto, Auto, 60, Auto, 60, Auto, Auto, 60, Auto"
RowSpacing="20">

<Label
Expand Down Expand Up @@ -149,6 +149,19 @@
<mct:IconTintColorBehavior TintColor="Purple" />
</Image.Behaviors>
</Image>

<!-- Applies tint color to Button images in iOS and Mac Catalyst pending #2040 -->
<Button
Grid.Row="9"
Grid.ColumnSpan="3"
HorizontalOptions="Center"
VerticalOptions="End"
Text="Button with IconTintColorBehavior"
ImageSource="https://api.nuget.org/v3-flatcontainer/communitytoolkit.maui/9.0.1/icon">
<Button.Behaviors>
<mct:IconTintColorBehavior TintColor="Red" />
</Button.Behaviors>
</Button>
</Grid>
</ScrollView>
</pages:BasePage>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Android.Graphics;
using Android.Widget;
using Microsoft.Maui.Platform;
using MButton = Google.Android.Material.Button.MaterialButton;
using AButton = Android.Widget.Button;
using AView = Android.Views.View;
using Color = Microsoft.Maui.Graphics.Color;
Expand Down Expand Up @@ -78,6 +79,13 @@ static void SetImageViewTintColor(ImageView image, Color? color)

static void SetButtonTintColor(AButton button, Color? color)
{
if (button is MButton mButton && color is not null)
{
mButton.IconTintMode = PorterDuff.Mode.SrcIn;
mButton.IconTint = new Android.Content.Res.ColorStateList(new int[][] { Array.Empty<int>() }, new int[] { color.ToPlatform() });
return;
}

var drawables = button.GetCompoundDrawables().Where(d => d is not null);

if (color is null)
Expand Down Expand Up @@ -122,10 +130,18 @@ void ClearTintColor(View element, AView control)
image.ClearColorFilter();
break;
case AButton button:

if (button is MButton mButton)
{
mButton.IconTint = null;
break;
}

foreach (var drawable in button.GetCompoundDrawables())
{
drawable?.ClearColorFilter();
}

break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ protected override void OnDetachedFrom(View bindable, FrameworkElement platformV
static bool TryGetButtonImage(WButton button, [NotNullWhen(true)] out WImage? image)
{
image = button.Content as WImage;

if (image == null && button.Content is Microsoft.UI.Xaml.Controls.Panel panel)
Comment thread
pictos marked this conversation as resolved.
Outdated
image = panel.Children?.FirstOrDefault(i => i is WImage) as WImage;

return image is not null;
}

Expand Down