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?
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]];
searchController.searchBar.tintColor = .white
UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).tintColor = .black
Note that searchBar can't be optional.
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()
Easiest in Swift 5:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .black
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With