I am currently trying to adjust the Xamarin.Forms iOS SearchBar
to show the "Cancel" button when I add text to the search bar (not through the keyboard, but a category click). I successfully did this with my code below. Now the issue is that, I want the "Cancel" button to be clickable once it shows up.
Currently I am doing the following:
Click my command that changes the Text of the searchbar -> works
Cancel button shows up -> works
Cancel button is clickable -> does not work. it is gray. I need to click the searchbar (so the keyboard shows up) to have it turn blue and clickable.
This is my code:
[assembly: ExportRenderer(typeof(CustomSearchBar), typeof(SearchBar_iOS))]
namespace Project.iOS.Renderers
{
public class SearchBar_iOS : SearchBarRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.SearchBar> e)
{
base.OnElementChanged(e);
var element = e.NewElement as CustomSearchBar;
if (Control != null && element != null)
{
this.Control.TextChanged += (s, ea) =>
{
//if (ea.SearchText == "")
this.Control.ShowsCancelButton = true;
};
this.Control.OnEditingStarted += (s, ea) => //when control receives focus
{
this.Control.ShowsCancelButton = true;
};
this.Control.OnEditingStopped += (s, ea) => //when control looses focus
{
this.Control.ShowsCancelButton = false;
};
Control.BackgroundColor = Color.Transparent.ToUIColor();
Control.Layer.CornerRadius = 10;
Control.ClipsToBounds = true;
Control.SearchBarStyle = UISearchBarStyle.Minimal;
}
}
Is there a call in my renderer that can force it to be clickable when text is added?