I want my searchBar's tint color to be white (meaning the cancel button be white). The cursor is not visible when the tint color is white. Is there a way to set cursor color separately?
-
1I posted a solution with detailed screenshots here: http://stackoverflow.com/a/42444940/4754881 – Derek Soike Feb 24 '17 at 17:52
12 Answers
Set your tint color to the color you want the cancel button to be and then use the UIAppearance Protocol to change the tint color on the text field to be the color you wish the cursor to be. Ex:
[self.searchBar setTintColor:[UIColor whiteColor]];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTintColor:[UIColor darkGrayColor]];

- 1,610
- 1
- 16
- 19
-
2Don't know why, by this code (second line) changes also tint for barButton in next modal scene. So I used @Felipe's answer below. – zalogatomek Aug 08 '16 at 11:58
-
I had the same problem as tzaloga. Anyone any idea why this affects modal scene's bar button item color? – hanjustin Jan 21 '17 at 03:27
-
If you're fond of functional, yet annoying one-liners in Swift, I got Benjamin's for loop down to this:
searchController.searchBar.tintColor = UIColor.whiteColor()
searchController.searchBar.subviews[0].subviews.flatMap(){ $0 as? UITextField }.first?.tintColor = UIColor.blueColor()

- 239
- 3
- 11
Easiest in Swift 5:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .black

- 772
- 1
- 21
- 31
Compact Swift 2.0 solution using for-where syntax (no need to break loop):
// Make SearchBar's tint color white to get white cancel button.
searchBar.tintColor = UIColor.white()
// Loop into it's subviews and find TextField, change tint color to something else.
for subView in searchBar.subviews[0].subviews where subView.isKindOfClass(UITextField) {
subView.tintColor = UIColor.darkTextColor()
}

- 61
- 1
- 3
-
1This worked for me, when I wanted to change cursor color for only one searchBar. – zalogatomek Aug 08 '16 at 11:49
searchBar.tintColor = [UIColor whiteColor];
searchBar.backgroundColor = [UIColor clearColor];
for ( UIView *v in [searchBar.subviews.firstObject subviews] )
{
if ( YES == [v isKindOfClass:[UITextField class]] )
{
[((UITextField*)v) setTintColor:[UIColor blueColor]];
break;
}
}

- 799
- 12
- 17
This seemed to work for me in swift as well.
searchController.searchBar.tintColor = UIColor.whiteColor()
UITextField.appearanceWhenContainedInInstancesOfClasses([searchController.searchBar.dynamicType]).tintColor = UIColor.blackColor()

- 546
- 1
- 9
- 14
-
Both Felipe & Eric's solutions work for me but I feel more comfortable with your solution Eric. – David West Aug 08 '16 at 15:51
For those looking to do the same in Swift, here is a solution I came accros after quite a lot of trouble :
override func viewWillAppear(animated: Bool) {
self.searchBar.tintColor = UIColor.whiteColor()
let view: UIView = self.searchBar.subviews[0] as! UIView
let subViewsArray = view.subviews
for (subView: UIView) in subViewsArray as! [UIView] {
println(subView)
if subView.isKindOfClass(UITextField){
subView.tintColor = UIColor.blueColor()
}
}
}

- 8,128
- 3
- 34
- 45
I would just add an extension to UISearchBar with the following code.
extension UISearchBar {
var cursorColor: UIColor! {
set {
for subView in self.subviews[0].subviews where ((subView as? UITextField) != nil) {
subView.tintColor = newValue
}
}
get {
for subView in self.subviews[0].subviews where ((subView as? UITextField) != nil) {
return subView.tintColor
}
// Return default tintColor
return UIColor.eightBit(red: 1, green: 122, blue: 255, alpha: 100)
}
}
}

- 841
- 6
- 13
iOS 9 and above
The easiest way to set a tintColor different for cancel button and textField, use this:
[self.searchBar setTintColor:[UIColor whiteColor]];
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:UIColor.blueColor];

- 2,115
- 2
- 25
- 22
-
1You are missing a [ at the start of your first line... Should be : [self.searchBar setTintColor:[UIColor whiteColor]]; – Peter Suwara Aug 16 '19 at 12:27
This is the easiest solution.
let textField = self.searchBar.value(forKey: "searchField") as! UITextField
textField.tintColor = UIColor.white

- 11
- 1
-
`let textField = self.searchBar.value(forKey: "searchField") as? UITextField; textField?.tintColor = UIColor.white` avoids forced unwrapping and thereby any unwanted crashes. – idrougge Mar 14 '19 at 12:25
Here is a "functional" version of Felipe's answer (Swift 4.2)
// Make SearchBar's tint color white to get white cancel button.
searchBar.tintColor = .white
// Get the TextField subviews, change tint color to something else.
if let textFields = searchBar.subviews.first?.subviews.compactMap({ $0 as? UITextField }) {
textFields.forEach { $0.tintColor = UIColor.darkGray }
}

- 881
- 8
- 19

- 5,905
- 4
- 56
- 71