Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 429
SlopTonio Avatar asked Dec 26 '15 16:12

SlopTonio


1 Answers

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()
like image 168
shahil Avatar answered Oct 05 '22 23:10

shahil