Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing cursor color on UISearchBar without changing tintColor

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?

like image 566
Ashish Awaghad Avatar asked Jul 29 '14 04:07

Ashish Awaghad


4 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]];
like image 52
McFadden Avatar answered Nov 20 '22 07:11

McFadden


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.

like image 15
Fangming Avatar answered Nov 20 '22 07:11

Fangming


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()
like image 13
iantheparker Avatar answered Nov 20 '22 07:11

iantheparker


Easiest in Swift 5:

    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .black
like image 8
Joshua Hart Avatar answered Nov 20 '22 08:11

Joshua Hart