42

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?

1

12 Answers 12

67

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]];
3
  • 2
    Don't know why, by this code (second line) changes also tint for barButton in next modal scene. So I used @Felipe's answer below. Aug 8, 2016 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, 2017 at 3:27
  • since iOS13, you can use searchBar.searchTextField.tintColor Dec 6, 2021 at 17:53
17

Swift 3.0 and 4 version

searchController.searchBar.tintColor = .white        
UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).tintColor = .black

Note that searchBar can't be optional.

13

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()
8

Easiest in Swift 5:

    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .black
1
6

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()
}
1
  • 1
    This worked for me, when I wanted to change cursor color for only one searchBar. Aug 8, 2016 at 11:49
5
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;
    }
}

enter image description here

5

This seemed to work for me in swift as well.

    searchController.searchBar.tintColor = UIColor.whiteColor()
    UITextField.appearanceWhenContainedInInstancesOfClasses([searchController.searchBar.dynamicType]).tintColor = UIColor.blackColor()
1
  • Both Felipe & Eric's solutions work for me but I feel more comfortable with your solution Eric.
    – David West
    Aug 8, 2016 at 15:51
4

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()
        }
    }

}
3

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)
        }
    }
}
3

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];
1
  • 1
    You are missing a [ at the start of your first line... Should be : [self.searchBar setTintColor:[UIColor whiteColor]]; Aug 16, 2019 at 12:27
1

This is the easiest solution.

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

    textField.tintColor = UIColor.white
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, 2019 at 12:25
1

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 }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.