SlopTonio
SlopTonio

Reputation: 1105

Change UISearchbar Magnifying Glass Color, PlaceHolder Color, and X Color

I've searched various solutions to accomplish this task but they are either in objective C or involve replacing the magnifying glass image.

enter image description here

enter image description here

Previous Posts I looked at:

Change color of magnifying glass

How to change UISearchBar Placeholder and image tint color?

The reason I dont want to replace the image of the magnifying glass is because the page color is dynamic and there are over 100+ color combinations

Any help on changing the UISearchbar Magnifying Glass Color, PlaceHolder Color, and X Color would greatly be appreciated

Upvotes: 5

Views: 7673

Answers (4)

Fattie
Fattie

Reputation: 12363

For 2024

class UsableUISearchBar: UISearchBar {
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        common()
    }

    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
        common()
    }

    func common() {
        barTintColor = .clear
        searchTextField.backgroundColor = .darkGray
        searchTextField.leftView?.tintColor = .white
    }
}

Yes, init seems to be late enough to set the colors. (Obviously, depending on your house style you could do it in layout.)

Upvotes: 0

Stephanie Lisker
Stephanie Lisker

Reputation: 1

func setupPlaceHolder(text: String, textColor: UIColor) {
  searchBar.searchTextField.attributedPlaceholder = NSAttributedString(
    string: text,
    attributes: [.foregroundColor: textColor]
  )
        
  searchBar.searchTextField.leftView?.tintColor = .white
}

Upvotes: 0

Kqtr
Kqtr

Reputation: 5935

Shahil's answer updated for Swift 3:

let textField = searchBar.value(forKey: "searchField") as! UITextField

let glassIconView = textField.leftView as! UIImageView
glassIconView.image = glassIconView.image?.withRenderingMode(.alwaysTemplate)
glassIconView.tintColor = .white


let clearButton = textField.value(forKey: "clearButton") as! UIButton
clearButton.setImage(clearButton.imageView?.image?.withRenderingMode(.alwaysTemplate), for: .normal)
clearButton.tintColor = .white

Upvotes: 3

shahil
shahil

Reputation: 1001

Objective C :

NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];
for (UIView *view in searchBarSubViews) {
    if([view isKindOfClass:[UITextField class]])
    {
        UITextField *textField = (UITextField*)view;
        UIImageView *imgView = (UIImageView*)textField.leftView;
        imgView.image = [imgView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        imgView.tintColor = [UIColor whiteColor];

        UIButton *btnClear = (UIButton*)[textField valueForKey:@"clearButton"];
        [btnClear setImage:[btnClear.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
        btnClear.tintColor = [UIColor whiteColor];

    }
}
[self.searchBar reloadInputViews];

Swift :

// Text field in search bar.
let textField = searchController.searchBar.valueForKey("searchField") as! UITextField

let glassIconView = textField.leftView as! UIImageView
glassIconView.image = glassIconView.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
glassIconView.tintColor = UIColor.whiteColor()

let clearButton = textField.valueForKey("clearButton") as! UIButton
clearButton.setImage(clearButton.imageView?.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate), forState: .Normal)
clearButton.tintColor = UIColor.whiteColor()

Upvotes: 15

Related Questions