Search code examples
xamarin.formssearchbar

How to change the color of searchbar search icon and cancel button color in xamarin forms


How to change the color of searchbar icon from xaml, I want to change the cancel and search icon color of a search bar in xamarin forms application.How to implement this. Please help on this


Solution

  • As adamm said, you can modify the "cancel button color" via CancelButtonColor.

    Similarly, if you want to implement a custom SearchBar in iOS, you can also create a custom renderer for it.

    For UISearchBar, you can modify the color of the icon via SearchTextField.LeftView.TintColor.

    [assembly: ExportRenderer(typeof(MySearchBar), typeof(MySearchBarRenderer))]
    namespace searchbar.iOS
    {
        public class MySearchBarRenderer : SearchBarRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
            {
                base.OnElementChanged(e);
    
                if (Control != null)
                {
                    Control.BackgroundColor = UIColor.Yellow;
                    UISearchBar searchbar = Control as UISearchBar;
                    searchbar.SearchTextField.LeftView.TintColor = UIColor.Orange;
                    // UPDATE
                    var clearButton = searchbar.SearchTextField.ValueForKey((Foundation.NSString)"clearButton") as UIButton;
                    //clearButton.SetTitleColor(UIColor.Blue, UIControlState.Normal);
                    //clearButton.TintColor = UIColor.Orange;
                    clearButton.SetImage(UIImage.FromBundle("CloseIcon.png"), UIControlState.Normal);
                }
            }
        }
    }