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

[iOS] Fix Background issue in Button CustomRenderer #12395

Merged
merged 6 commits into from
Dec 15, 2020
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,89 @@
using CoreGraphics;
using System.ComponentModel;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.ControlGallery.iOS.CustomRenderers;
using Xamarin.Forms.Controls.Issues;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(Issue12372Button), typeof(_12372CustomRenderer))]
namespace Xamarin.Forms.ControlGallery.iOS.CustomRenderers
{
public class _12372CustomRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> args)
{
base.OnElementChanged(args);

if (Control != null && Element != null)
{
SetColors();
Control.TitleLabel.LineBreakMode = UILineBreakMode.WordWrap;
Control.TitleLabel.TextAlignment = UITextAlignment.Center;
}
}

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

if (Control == null || Element == null || args == null)
return;

if (args.PropertyName == nameof(Button.IsEnabled) ||
args.PropertyName == nameof(Button.IsPressed) ||
args.PropertyName == nameof(Button.IsVisible))
{
SetColors();
}
}

private void SetColors()
{
if (Element is Issue12372Button nymblButton)
{
Control.SetTitleColor(nymblButton.NymblTextColor.ToUIColor(), UIControlState.Normal);
Control.SetTitleColor(nymblButton.NymblPressedColor.ToUIColor(), UIControlState.Selected);
Control.SetTitleColor(nymblButton.NymblDisabledTextColor.ToUIColor(), UIControlState.Disabled);

if (nymblButton.IsEnabled && !nymblButton.IsPressed)
{
Control.BackgroundColor = nymblButton.NymblDefaultColor.ToUIColor();
CreateShadow();
}
else if (nymblButton.IsEnabled && nymblButton.IsPressed)
{
Control.BackgroundColor = nymblButton.NymblPressedColor.ToUIColor();
RemoveShadow();
}
else if (!nymblButton.IsEnabled)
{
Control.BackgroundColor = nymblButton.NymblDisabledColor.ToUIColor();
RemoveShadow();
}
else
{
// Any other state?
Control.BackgroundColor = nymblButton.NymblDefaultColor.ToUIColor();
RemoveShadow();
}
}
}

private void CreateShadow()
{
Layer.CornerRadius = 20;
Layer.ShadowRadius = 2;
Layer.ShadowColor = UIColor.Black.CGColor;
Layer.ShadowOffset = new CGSize(4, 4);
Layer.ShadowOpacity = 0.80f;
}

private void RemoveShadow()
{
Layer.CornerRadius = 20;
Layer.ShadowOpacity = 0f;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
<Compile Include="CustomEffects\FooEffect.cs" />
<Compile Include="_10337CustomRenderer.cs" />
<Compile Include="_11132CustomRenderer.cs" />
<Compile Include="CustomRenderers\_12372CustomRenderer.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Xamarin.Forms.Controls\Xamarin.Forms.Controls.csproj">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

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

namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
[Category(UITestCategories.ManualReview)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 12372, "[Bug] XF 4.8 breaks custom renderer (Button) background color on iOS", PlatformAffected.iOS)]
public class Issue12372 : TestContentPage
{
public Issue12372()
{

}

protected override void Init()
{
Title = "Issue 12372";

var layout = new StackLayout();

var instructions = new Label
{
Padding = 12,
BackgroundColor = Color.Black,
TextColor = Color.White,
Text = "If you can see the background color of the custom Button below, the test has passed."
};

var button = new Issue12372Button
{
NymblDefaultColor = Color.Blue,
NymblPressedColor = Color.Red,
NymblTextColor = Color.White,
Text = "Issue12372"
};

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

Content = layout;
}
}

public class Issue12372Button : Button
{
public readonly static BindableProperty NymblDefaultColorProperty = BindableProperty.Create(
nameof(NymblDefaultColor),
typeof(Color),
typeof(Issue12372Button),
Color.Blue);

public Color NymblDefaultColor
{
get => (Color)GetValue(NymblDefaultColorProperty);
set => SetValue(NymblDefaultColorProperty, value);
}

public readonly static BindableProperty NymblPressedColorProperty = BindableProperty.Create(
nameof(NymblPressedColor),
typeof(Color),
typeof(Issue12372Button),
Color.Blue);

public Color NymblPressedColor
{
get => (Color)GetValue(NymblPressedColorProperty);
set => SetValue(NymblPressedColorProperty, value);
}

public readonly static BindableProperty NymblDisabledColorProperty = BindableProperty.Create(
nameof(NymblDisabledColor),
typeof(Color),
typeof(Issue12372Button),
Color.Blue);

public Color NymblDisabledColor
{
get => (Color)GetValue(NymblDisabledColorProperty);
set => SetValue(NymblDisabledColorProperty, value);
}

public readonly static BindableProperty NymblDisabledTextColorProperty = BindableProperty.Create(
nameof(NymblDisabledTextColor),
typeof(Color),
typeof(Issue12372Button),
Color.LightSlateGray);

public Color NymblDisabledTextColor
{
get => (Color)GetValue(NymblDisabledTextColorProperty);
set => SetValue(NymblDisabledTextColorProperty, value);
}

public readonly static BindableProperty NymblBorderColorProperty = BindableProperty.Create(
nameof(NymblBorderColor),
typeof(Color),
typeof(Issue12372Button),
Color.White);

public Color NymblBorderColor
{
get => (Color)GetValue(NymblBorderColorProperty);
set => SetValue(NymblBorderColorProperty, value);
}

public readonly static BindableProperty NymblBorderWidthProperty = BindableProperty.Create(
nameof(NymblBorderWidth),
typeof(int),
typeof(Issue12372Button));

public int NymblBorderWidth
{
get => (int)GetValue(NymblBorderWidthProperty);
set => SetValue(NymblBorderWidthProperty, value);
}

public readonly static BindableProperty NymblTextColorProperty = BindableProperty.Create(
nameof(NymblTextColor),
typeof(Color),
typeof(Issue12372Button),
Color.White);

public Color NymblTextColor
{
get => (Color)GetValue(NymblTextColorProperty);
set => SetValue(NymblTextColorProperty, value);
}

public readonly static BindableProperty NymblTextAllCapsProperty = BindableProperty.Create(
nameof(NymblTextAllCaps),
typeof(bool),
typeof(Issue12372Button));

public bool NymblTextAllCaps
{
get => (bool)GetValue(NymblTextAllCapsProperty);
set => SetValue(NymblTextAllCapsProperty, value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue11081.xaml.cs">
<DependentUpon>Issue11081.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue12372.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue12374.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue12222.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue12574.cs" />
Expand Down
11 changes: 5 additions & 6 deletions Xamarin.Forms.Platform.iOS/Renderers/ButtonRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using Foundation;
using UIKit;
using Xamarin.Forms.Internals;
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
using Specifics = Xamarin.Forms.PlatformConfiguration.iOSSpecific;
using SizeF = CoreGraphics.CGSize;
using PreserveAttribute = Foundation.PreserveAttribute;
using CoreGraphics;
Expand Down Expand Up @@ -86,7 +81,11 @@ public override void LayoutSubviews()
{
if (_previousSize != Bounds.Size)
{
SetBackground(Element.Background);
Brush brush = Element.Background;

if (!Brush.IsNullOrEmpty(brush))
SetBackground(brush);

SetNeedsDisplay();
}

Expand Down
5 changes: 4 additions & 1 deletion Xamarin.Forms.Platform.iOS/ViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,10 @@ void UpdateBackground()
if (IsElementOrControlEmpty)
return;

SetBackground(Element.Background);
Brush brush = Element.Background;

if (!Brush.IsNullOrEmpty(brush))
SetBackground(brush);
}

void UpdateIsEnabled()
Expand Down