DiddanDo
DiddanDo

Reputation: 401

How to make "Cancel" on iOS searchbar clickable when keyboard is not focused? [Xamarin Forms]

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?

Upvotes: 0

Views: 531

Answers (1)

jgoldberger - MSFT
jgoldberger - MSFT

Reputation: 6098

This was already discussed for iOS native here: How do you keep the cancel button in the search bar enabled when the keyboard is dismissed?

However it appears that iOS does not want you messing with the behavior of the Cancel button otherwise they would have exposed it publicly. That said, try this in your custom renderer for the OnEditingStopped event (quick test on my end worked):

 this.Control.OnEditingStopped += async (s, ea) => //when control looses focus 
 {
      this.Control.ShowsCancelButton = false;
      var searchBar = this.Control;
      BeginInvokeOnMainThread(() =>
      {
          var cancelButton = searchBar.ValueForKey(new NSString("cancelButton")) as UIButton;
          cancelButton.Enabled = true;
       });
  };

There is a risk of Apple rejection or other undiscovered issues, so don't yell at me if it does not work. :-)

Also, since the cancel button is not made publicly accessible with out getting to it via the ValueForKey method, Apple could change the key name at any time which would break this hacky workaround.

Upvotes: 1

Related Questions