52 votes

iOS 11 personnaliser la barre de recherche dans la barre de navigation

Je veux changer la couleur du texte et l'icône de l'iOS 11 barre de recherche quand il est intégré dans la barre de navigation. Si l'espace réservé de texte, le texte de la recherche et de l'icône de recherche.

enter image description here

if #available(iOS 11.0, *) {
    navigationController?.navigationBar.prefersLargeTitles = false
    let searchController = UISearchController(searchResultsController: nil)
    navigationItem.searchController = searchController
    navigationItem.hidesSearchBarWhenScrolling = false

    searchController.searchBar.placeholder = "Suchen"
    searchController.searchBar.tintColor = .white
}

Comme vous pouvez le voir dans l'image, le texte est gris sur un arrière-plan bleu profond, qui semble laid. Je veux le texte et l'icône d'être au moins blanc. (en changeant le bleu de la couleur d'arrière-plan ne fonctionne pas vraiment bon, voir mon autre question)

La seule chose qui fonctionne est en train de changer la couleur du curseur qui clignote et le bouton "annuler", qui est fait avec l' .nuance de couleur à la propriété.

Des Solutions qui semble fonctionner dans iOS 10 et ci-dessous semblent ne fonctionne plus sous iOS 11, donc merci de ne poster que des solutions qui vous savez travailler dans iOS 11. Merci.

Peut-être que je rate le point sur ce "style automatique" dans iOS 11. Toute aide est appréciée.

122voto

Darko Points 1025

Je viens de trouver comment mettre aussi le reste d'entre eux: (avec l'aide de Brandon, merci!)

Le bouton "Annuler" texte:

searchController.searchBar.tintColor = .white

L'icône de recherche:

searchController.searchBar.setImage(UIImage(named: "my_search_icon"), for: UISearchBarIcon.search, state: .normal)

L'icône effacer:

searchController.searchBar.setImage(UIImage(named: "my_search_icon"), for: UISearchBarIcon.clear, state: .normal)

Le texte de la recherche:

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white]

Merci pour l'aide @Brandon!

enter image description here

L'espace réservé:

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])

enter image description here

Le fond blanc:

if #available(iOS 11.0, *) {
    let sc = UISearchController(searchResultsController: nil)
    sc.delegate = self
    let scb = sc.searchBar
    scb.tintColor = UIColor.white
    scb.barTintColor = UIColor.white

    if let textfield = scb.value(forKey: "searchField") as? UITextField {
        textfield.textColor = UIColor.blue
        if let backgroundview = textfield.subviews.first {

            // Background color
            backgroundview.backgroundColor = UIColor.white

            // Rounded corner
            backgroundview.layer.cornerRadius = 10;
            backgroundview.clipsToBounds = true;
        }
    }

    if let navigationbar = self.navigationController?.navigationBar {
        navigationbar.barTintColor = UIColor.blue
    }
    navigationItem.searchController = sc
    navigationItem.hidesSearchBarWhenScrolling = false
}

enter image description here

Prises à partir d' ici.

3voto

Brandon Points 4211

Mettre

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.white]

et

UISearchBar.appearance().tintColor = UIColor.white dans le AppDelegate .

Sinon, mettez les deux en [UIViewController viewDidLoad:]

3voto

Alex Kolovatov Points 462

En plus de Darko de réponse. Dans mon cas, j'ai besoin de pour obtenir d'un blanc pur de recherche textfield couleur. Aussi j'ai besoin d'un custom borderLine et cornerRadius. Donc, si j'ai juste mis en arrière-plan de couleur blanc, personnaliser le rayon de l'angle et de la coutume ligne de frontière, j'ai quelque chose comme ça. Look at this ugly grey highLight when you tap on textfield

Le problème est que la barre de recherche a certains sous-vues et j'ai juste enlevé eux. Voici mon code:

@interface YourViewController () <UISearchBarDelegate, UISearchControllerDelegate>
// your properties
@property (nonatomic,strong) UISearchController *searchController;
@property (nonatomic,strong) UISearchBar *searchBar;
@end

- (void)viewDidLoad {
[super viewDidLoad];

_searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
_searchController.delegate = self;
_searchController.searchBar.delegate = self;
_searchBar = self.searchController.searchBar;

if (@available(iOS 11.0, *)) {

    UITextField *searchTextField = [_searchBar valueForKey:@"searchField"];
if (searchTextField != nil) {
        searchTextField.layer.cornerRadius = 4.f;
        searchTextField.layer.borderWidth = 1.f;
        searchTextField.clipsToBounds = YES;

        for (UIView *subView in searchTextField.subviews) {
            [subView removeFromSuperview];
        }
}

// Color for "Cancel" button
    _searchBar.tintColor = [UIColor blackColor];
// Add searchController to navgationBar
    _navigationItem.searchController = _searchController;
// Hide searchBar when scroll
    _navigationItem.hidesSearchBarWhenScrolling = YES;
}
}

Maintenant, j'ai une barre de recherche avec le blanc pur de fond, la coutume cornerRadius, personnalisé largeur de la bordure. Aussi j'ai désactivé gris en surbrillance lorsque le robinet. Editing searchfield Resign first responder

2voto

Donny Points 101

Définir la couleur du texte de recherche

 (UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]) ).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
 

Définir la couleur de l'espace réservé pour la recherche

 (UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]) ).attributedPlaceholder = [NSForegroundColorAttributeName: UIColor.white]
 

1voto

ingconti Points 405

mes deux cents pour Swift 4.x, légèrement nettoyé.

Ajoutez un contrôleur ou un délégué d'application:

 appearance.backgroundColor = .green
let myFont = UIFont.italicSystemFont(ofSize: 12)
let attribs = [
    NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue): myFont,
    NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.red
]

appearance.defaultTextAttributes =  attribs
 

dans le contrôleur:

 self.searchBar.barTintColor = .blue
 

Vous obtiendrez un fond bleu, un fond de barre de recherche vert, une police en italique rouge:

entrez la description de l'image ici

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X