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

Commit

Permalink
Fix crashing on iOS < 14 if ClearButtonVisibility = WhileEditing in E…
Browse files Browse the repository at this point in the history
…ntry (#14479 #14510) (#14526)
  • Loading branch information
rotorgames committed Oct 8, 2021
1 parent b3aaa0b commit a21b36a
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions Xamarin.Forms.Platform.iOS/Renderers/EntryRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using UIKit;
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
using Specifics = Xamarin.Forms.PlatformConfiguration.iOSSpecific.Entry;
using RectangleF = CoreGraphics.CGRect;

namespace Xamarin.Forms.Platform.iOS
{
Expand Down Expand Up @@ -51,6 +50,8 @@ public abstract class EntryRendererBase<TControl> : ViewRenderer<Entry, TControl

UIImage _defaultClearImage;

UIButton ClearButton => Control?.ValueForKey(new NSString("clearButton")) as UIButton;

public EntryRendererBase()
{
}
Expand Down Expand Up @@ -91,6 +92,8 @@ protected override void Dispose(bool disposing)
Control.EditingDidEnd -= OnEditingEnded;
Control.ShouldChangeCharacters -= ShouldChangeCharacters;
_selectedTextRangeObserver?.Dispose();

ClearButton?.Layer?.RemoveObserver(this, new NSString("sublayers"));
}
}

Expand Down Expand Up @@ -124,6 +127,8 @@ protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
textField.EditingDidEnd += OnEditingEnded;
textField.ShouldChangeCharacters += ShouldChangeCharacters;
_selectedTextRangeObserver = textField.AddObserver("selectedTextRange", NSKeyValueObservingOptions.New, UpdateCursorFromControl);

ClearButton?.Layer.AddObserver(this, new NSString("sublayers"), NSKeyValueObservingOptions.New, IntPtr.Zero);
}

// When we set the control text, it triggers the UpdateCursorFromControl event, which updates CursorPosition and SelectionLength;
Expand Down Expand Up @@ -155,6 +160,12 @@ protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
UpdateClearButtonVisibility();
}

public override void ObserveValue(NSString keyPath, NSObject ofObject, NSDictionary change, IntPtr context)
{
if (keyPath == new NSString("sublayers") && _defaultClearImage == null)
UpdateClearButtonVisibility();
}

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == Entry.PlaceholderProperty.PropertyName || e.PropertyName == Entry.PlaceholderColorProperty.PropertyName)
Expand Down Expand Up @@ -565,36 +576,33 @@ void UpdateClearButtonVisibility()

void UpdateClearButtonColor()
{
if (Control.ValueForKey(new NSString("clearButton")) is UIButton clearButton)
if (ClearButton != null)
{
clearButton.TintColor = Element.TextColor.ToUIColor();

ClearButton.TintColor = Element.TextColor.ToUIColor();
if(_defaultClearImage == null)
_defaultClearImage = clearButton.ImageForState(UIControlState.Highlighted);
_defaultClearImage = ClearButton.ImageForState(UIControlState.Highlighted);

if (_defaultClearImage == null)
return;

if (Element.TextColor == Color.Default)
if(Element.TextColor == Color.Default)
{
clearButton.SetImage(_defaultClearImage, UIControlState.Normal);
clearButton.SetImage(_defaultClearImage, UIControlState.Highlighted);
ClearButton.SetImage(_defaultClearImage, UIControlState.Normal);
ClearButton.SetImage(_defaultClearImage, UIControlState.Highlighted);
}
else
{
var tintedClearImage = GetClearButtonTintImage(_defaultClearImage, Element.TextColor.ToUIColor());

if (tintedClearImage != null)
{
clearButton.SetImage(tintedClearImage, UIControlState.Normal);
clearButton.SetImage(tintedClearImage, UIControlState.Highlighted);
}
ClearButton.SetImage(tintedClearImage, UIControlState.Normal);
ClearButton.SetImage(tintedClearImage, UIControlState.Highlighted);
}
}
}

UIImage GetClearButtonTintImage(UIImage image, UIColor color)
{
if (image == null)
return null;

var size = image.Size;

UIGraphics.BeginImageContextWithOptions(size, false, UIScreen.MainScreen.Scale);
Expand Down

0 comments on commit a21b36a

Please sign in to comment.