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 .
