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

[iOS] Fix gradient background issue on Image #14797

Merged
merged 2 commits into from
Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;


#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
using Xamarin.Forms.Core.UITests;
#endif

namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 14397, "[Bug] iOS Background appears in front of image",
PlatformAffected.iOS)]
#if UITEST
[NUnit.Framework.Category(UITestCategories.Image)]
#endif
public class Issue14397 : TestContentPage
{
public Issue14397()
{
Title = "Issue 14397";

var layout = new StackLayout();

var instructions = new Label
{
Padding = 12,
Text = "If can see the icon with the gradient background, the test has passed."
};

var image = new Image
{
HorizontalOptions = LayoutOptions.Start,
HeightRequest = 100,
WidthRequest = 100,
Source = "coffee.png",
Margin = new Thickness(12, 0)
};

image.Background = new LinearGradientBrush
{
StartPoint = new Point(0, 0),
EndPoint = new Point(1, 0),
GradientStops = new GradientStopCollection
{
new GradientStop { Color = Color.OrangeRed, Offset = 0.0f },
new GradientStop { Color = Color.PaleVioletRed, Offset = 0.9f },
}
};

layout.Children.Add(instructions);
layout.Children.Add(image);

Content = layout;
}

protected override void Init()
{

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1827,6 +1827,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue14505-II.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue6387.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14757.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14397.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14897.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14805.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11954.cs" />
Expand Down
15 changes: 13 additions & 2 deletions Xamarin.Forms.Platform.iOS/Renderers/ImageRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Drawing;
using System.ComponentModel;
using System.IO;
using System.Threading;
Expand Down Expand Up @@ -72,6 +71,7 @@ protected override async void OnElementChanged(ElementChangedEventArgs<Image> e)
}

await TrySetImage(e.OldElement as Image);
UpdateBackground();
}

base.OnElementChanged(e);
Expand All @@ -83,6 +83,8 @@ protected override async void OnElementPropertyChanged(object sender, PropertyCh

if (e.PropertyName == Image.SourceProperty.PropertyName)
await TrySetImage().ConfigureAwait(false);
else if (e.PropertyName == VisualElement.BackgroundProperty.PropertyName)
UpdateBackground();
}

protected virtual async Task TrySetImage(Image previous = null)
Expand Down Expand Up @@ -115,8 +117,17 @@ protected async Task SetImage(Image oldElement = null)
bool IImageVisualElementRenderer.IsDisposed => _isDisposed;

UIImageView IImageVisualElementRenderer.GetImage() => Control;
}

void UpdateBackground()
{
var parent = Control.Superview;

if (parent == null)
return;

parent.UpdateBackground(Element.Background);
}
}

public interface IImageSourceHandler : IRegisterable
{
Expand Down
4 changes: 4 additions & 0 deletions Xamarin.Forms.Platform.iOS/ViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ protected override void SetBackground(Brush brush)
if (IsElementOrControlEmpty)
return;

// Updated in the ImageRenderer
if (Element is Image)
return;

Control.UpdateBackground(brush);
}

Expand Down