Xamarin IOS hide the search bar icon based on IsFocused property in custom renderer

Viewed 704

I wanted to hide the search bar icon in IOS using custom SearchBarRenderer based on the IsFocused property. How can I achieve this?

protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

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

            var element = Element as CustomSearchBar;

            if (element.IsFocused)
            {

            }
            else
            {

            }
        }
2 Answers

Personal note - there is no need to use a Custom Renderer for this, an effect is more than enough - I tested with Effect, not with custom renderer. On how to create Effects, see here

this.Control will be a UISearchBar if you only add this effect to <SearchBar> elements.

So you can do this (based on iOS and Xamarin.iOS documentation):

iOS implementation of effect:

public class NoSearchIconEffect : PlatformEffect
{
    UIImage defaultIcon = null;

    protected override void OnAttached()
    {
        var searchBar = this.Control as UISearchBar;
        defaultIcon = defaultIcon ?? searchBar.GetImageForSearchBarIcon(UISearchBarIcon.Search, UIControlState.Normal); //This will save the default icon

        searchBar.OnEditingStarted += delegate
        {
            searchBar.SetImageforSearchBarIcon(new UIImage(), UISearchBarIcon.Search, UIControlState.Normal);
        };

        searchBar.OnEditingStopped += delegate
        {
            searchBar.SetImageforSearchBarIcon(defaultIcon, UISearchBarIcon.Search, UIControlState.Normal);
        };
    }

    protected override void OnDetached()
    {

    }
}

If you are using Custom Renderer to realize it , code as follow .

[assembly: ExportRenderer(typeof(CustomSearchBar), typeof(CustomSearchBarRenderer))]
namespace App15.iOS
{
    public class CustomSearchBarRenderer : SearchBarRenderer
    {
        UIImage searchImg;
        protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
        {
            base.OnElementChanged(e);
            searchImg = Control.GetImageForSearchBarIcon(UISearchBarIcon.Search, UIControlState.Normal);
            if (e.OldElement != null)
            {
                Control.OnEditingStarted -= Control_OnEditingStarted;
                Control.OnEditingStopped -= Control_OnEditingStopped;
            }

            if (e.NewElement != null)
            {
                if (null != Control)
                {
                    Control.OnEditingStarted += Control_OnEditingStarted;
                    Control.OnEditingStopped += Control_OnEditingStopped;
                }
            }
        }

        private void Control_OnEditingStopped(object sender, EventArgs e)
        {
            var searchBar = sender as UISearchBar;
            searchBar.SetImageforSearchBarIcon(searchImg, UISearchBarIcon.Search, UIControlState.Normal);
        }

        private void Control_OnEditingStarted(object sender, EventArgs e)
        {
            var searchBar = sender as UISearchBar;
            searchBar.SetImageforSearchBarIcon(new UIImage(), UISearchBarIcon.Search, UIControlState.Normal);
        }
    }
}

Note: App15 is namespace in my local project.

As document Introduction to Effects of effects said , you also can refer to this document Creating an Effect to use Effect to realize it ,because it no need to use Custom Renderer to do that .

Sample of Effects as follow:

Creating SearchBarEffects :

public class SearchBarEffects : RoutingEffect
{
    public SearchBarEffects() : base($"MyCompany.{nameof(SearchBarEffects)}")
    {

    }
}

Using in Xaml :

// here is custom renderer
<local:CustomSearchBar Placeholder="renderer input"/> 
// here is custom effect
<SearchBar Placeholder="effect input">
    <SearchBar.Effects>
        <local:SearchBarEffects />
    </SearchBar.Effects>
</SearchBar>

Creating SearchBarEffect in iOS solution :

[assembly: ResolutionGroupName("MyCompany")]
[assembly: ExportEffect(typeof(App15.iOS.SearchBarEffect), "SearchBarEffects")]
namespace App15.iOS
{
    public class SearchBarEffect : PlatformEffect
    {
        UIImage searchImg;
        protected override void OnAttached()
        {
            searchImg = ((UISearchBar)Control).GetImageForSearchBarIcon(UISearchBarIcon.Search, UIControlState.Normal);
            ((UISearchBar)Control).OnEditingStarted += SearchBar_OnEditingStarted;
            ((UISearchBar)Control).OnEditingStopped += SearchBar_OnEditingStopped;
        }

        private void SearchBar_OnEditingStopped(object sender, EventArgs e)
        {
            var searchBar = sender as UISearchBar;
            searchBar.SetImageforSearchBarIcon(searchImg, UISearchBarIcon.Search, UIControlState.Normal);
        }

        private void SearchBar_OnEditingStarted(object sender, EventArgs e)
        {
            var searchBar = sender as UISearchBar;
            searchBar.SetImageforSearchBarIcon(new UIImage(), UISearchBarIcon.Search, UIControlState.Normal);
        }

        protected override void OnDetached()
        {
            ((UISearchBar)Control).OnEditingStarted -= SearchBar_OnEditingStarted;
            ((UISearchBar)Control).OnEditingStopped -= SearchBar_OnEditingStopped;
        }
    }
}

Finally ,showing the effect by two ways . Up is Effect's , down is Renderer's .

enter image description here

Related