Cambiar el color del cursor en UISearchBar sin cambiar el color del tinte


Quiero que el color de tinte de mi barra de búsqueda sea blanco (lo que significa que el botón cancelar sea blanco). El cursor no es visible cuando el color del tinte es blanco. ¿Hay alguna forma de establecer el color del cursor por separado?

Author: Ashish Awaghad, 2014-07-29

10 answers

Establezca su color de tinte en el color que desea que sea el botón cancelar y luego use el Protocolo UIAppearance para cambiar el color de tinte en el campo de texto para que sea el color que desea que sea el cursor. Ex:

[self.searchBar setTintColor:[UIColor whiteColor]];                
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTintColor:[UIColor darkGrayColor]];
 60
Author: McFadden,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-07-29 19:59:15

Si te gustan los one-liners funcionales pero molestos en Swift, tengo el bucle for de Benjamin a esto:

searchController.searchBar.tintColor = UIColor.whiteColor()

searchController.searchBar.subviews[0].subviews.flatMap(){ $0 as? UITextField }.first?.tintColor = UIColor.blueColor()
 11
Author: iantheparker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-07 17:03:13

Swift 3.0 y 4 versión

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

Tenga en cuenta que la barra de búsqueda no puede ser opcional.

 6
Author: Fangming,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-22 12:22:16
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;
    }
}

introduzca la descripción de la imagen aquí

 5
Author: Roman Solodyashkin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-26 15:19:24

Solución compacta Swift 2.0 usando para-donde sintaxis (sin necesidad de romper el bucle):

// 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()
}
 5
Author: Felipe Nuila,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-10 16:26:11

Para aquellos que buscan hacer lo mismo en Swift, aquí hay una solución que vine accros después de muchos problemas:

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

}
 4
Author: Benjamin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-14 14:04:25

Esto parecía funcionar para mí también en swift.

    searchController.searchBar.tintColor = UIColor.whiteColor()
    UITextField.appearanceWhenContainedInInstancesOfClasses([searchController.searchBar.dynamicType]).tintColor = UIColor.blackColor()
 3
Author: Eric Hodgins,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-07 05:26:15

Simplemente agregaría una extensión a UISearchBar con el siguiente código.

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)
        }
    }
}
 2
Author: I make my mark,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-09 14:40:21

Esta es la solución más fácil.

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

    textField.tintColor = UIColor.white
 0
Author: Teodor Pavlov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-11 16:19:57

IOS 9 y superiores

La forma más fácil de establecer un tintColor diferente para el botón cancelar y el campo de texto, use esto:

self.searchBar setTintColor:[UIColor whiteColor]];
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:UIColor.blueColor];
 0
Author: Adam Studenic,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-19 09:33:12