Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the color of the icons in a UItextField inside a UISearchBar

I'm trying to customise the appearance of the search bar in my search controller.

Setting the background and text colors works fine but I just didn't find a way to change the color of the icons in the text field, specifically the magnifying glass and the x button.

I've found this Objective-C code which should do what I want but I'm struggling to translate it to Swift:

(EDIT: Skip to the first answer for the working Swift 3 solution.)

UITextField *searchBarTextField = [self.searchDisplayController.searchBar valueForKey:@"_searchField"];

// Magnifying glass icon.
UIImageView *leftImageView = (UIImageView *)searchBarTextField.leftView;
leftImageView.image = [LeftImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
leftImageView.tintColor = [UIColor whiteColor];

// Clear button
UIButton *clearButton = [searchBarTextField valueForKey:@"_clearButton"];
[clearButton setImage:[clearButton.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
clearButton.tintColor = [UIColor whiteColor];

My attempt to translate to Swift:

let textField = searchController.searchBar.valueForKey("searchField") as! UITextField
// These two work fine.
textField.backgroundColor = UIColor.blackColor()
textField.textColor = UIColor.blackColor()

var glassIcon = textField.leftView
// This would work.
//glassIcon.hidden = true

// This does not have any effect.
//glassIcon?.tintColor = UIColor.whiteColor()

// My attempt to translate, but it gives an error.
glassIcon.image? = UIImage.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)

 var clearButton = textField.valueForKey("clearButton")!
            clearButton.setImage(clearButton.imageWithRenderingMode(.AlwaysTemplate), forState: .Normal)
// This gives the error: "Cannot assign to property: 'clearButton' is immutable
clearButton.tintColor = UIColor.whiteColor()
  // Sorry for the weird formatting, it glitches here in the editor. 

The leftView does not seem to have an image property. How can I access that property as the Objective-C code does? Also, if there is a better to achieve what I want please let me know.

like image 773
nontomatic Avatar asked Feb 29 '16 12:02

nontomatic


1 Answers

Here is the solution:

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

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

let clearButton = textField.valueForKey("clearButton") as! UIButton
clearButton.setImage(clearButton.imageView?.image?.withRenderingMode(.alwaysTemplate), for: .normal)
clearButton.tintColor = UIColor.white
like image 189
nontomatic Avatar answered Apr 05 '23 20:04

nontomatic