change navigation bar Search placeholder text color

joaopalmajoaopalma Member ✭✭
edited July 2018 in Xamarin.iOS

I'm having trouble to change the text color of the placeholder

textFieldInsideSearchBar.AttributedPlaceholder = new NSAttributedString("Search", foregroundColor: UIColor.White);

I'm able to change any other attributes except this one.

Its a bug or I'm doing it wrong ?

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited July 2018

    I tried your code , it works fine on my side .

    How do you access the textField inside SearchBar ? Can you attach your code ?

    This is my code :

        UITextField textfield = null;
    
        foreach (UIView subView in searchBar.Subviews)
        {
            foreach (UIView ndLeveSubView in subView.Subviews)
            {
                if (ndLeveSubView is UITextField)
                {
                    textfield = ndLeveSubView as UITextField;
                    break;
                }
            }
        }
        textfield.AttributedPlaceholder = new NSAttributedString("Search", foregroundColor: UIColor.Red);
    
  • joaopalmajoaopalma Member ✭✭

    Very strange to work for you, I read somewhere its not possible to change the foregroundcolor

  • jonathannicholsjonathannichols USMember ✭✭

    To anyone else who stumbles on this. You can change the magnifying glass, placeholder, & cancel button image as below in iOS custom renderer.

    `protected override void OnElementChanged(ElementChangedEventArgs e)
    {
    base.OnElementChanged(e);
    if (Control != null)
    {
    Control.SearchBarStyle = UISearchBarStyle.Minimal;
    var searchbar = (UISearchBar)Control;
    if (e.NewElement != null)
    {

                    var searchTextField = searchbar.ValueForKey((Foundation.NSString)"_searchField") as UITextField;
                    var glassview = (searchTextField.LeftView) as UIImageView;
    
                    glassview.Image = glassview.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
                    glassview.TintColor = UIColor.White;
    
                    //x button
                    var clearButton = searchTextField.ValueForKey((Foundation.NSString)"_clearButton") as UIButton;
                    if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
                    {
                        clearButton.SetImage(UIImage.FromBundle("my_white_clear_button"), UIControlState.Normal);
                        clearButton.TintColor = UIColor.Red;
                        clearButton.SetTitleColor(UIColor.Red,UIControlState.Normal);
                    }
                    else {
                        clearButton.SetImage(clearButton.ImageView.Image, UIControlState.Normal);
                        clearButton.TintColor = UIColor.Red;
                    }
    
    
    
                    var searchButton = searchTextField.ValueForKey((Foundation.NSString)"_placeholderLabel") as UILabel;
                    searchButton.TextColor = UIColor.White;
    
                }
    
            }
        }`
    
Sign In or Register to comment.