Je veux personnaliser la barre de recherche, je veux dire la boîte blanche. Puis-je le faire? Y a-t-il de la documentation à ce sujet? Existe-t-il un moyen de masquer la boîte blanche, mais pas les lettres. Puis-je au moins rendre la boîte plus petite? Je veux dire moins de hauteur
Par IOS 5.0 vous avez la possibilité d'utiliser
[[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"searchbar.png"]forState:UIControlStateNormal];
Voici la solution que je cherchais: sous-classer un UISearchBar, et écraser la disposition des méthodes
- (void)layoutSubviews {
UITextField *searchField;
NSUInteger numViews = [self.subviews count];
for(int i = 0; i < numViews; i++) {
if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?
searchField = [self.subviews objectAtIndex:i];
}
}
if(!(searchField == nil)) {
searchField.textColor = [UIColor whiteColor];
[searchField setBackground: [UIImage imageNamed:@"buscador.png"] ];
[searchField setBorderStyle:UITextBorderStyleNone];
}
[super layoutSubviews];
}
// Search Bar Customization
// Background Image
[self.searchDisplayController.searchBar setBackgroundImage:[[UIImage alloc]init]];
// Backgroud Color
[self.searchDisplayController.searchBar setBackgroundColor:[UIColor redColor]];
// Search Bar Cancel Button Color
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];
// set Search Bar Search icon
[self.searchDisplayController.searchBar setImage:[UIImage imageNamed:@"search_ico.png"]
forSearchBarIcon:UISearchBarIconSearch
state:UIControlStateNormal];
// set Search Bar textfield background image
[[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"search_box.png"]
forState:UIControlStateNormal];
// set Search Bar texfield corder radius
UITextField *txfSearchField = [self.searchDisplayController.searchBar valueForKey:@"_searchField"];
txfSearchField.layer.cornerRadius = 10.8f;
Vous pouvez changer l'arrière-plan de UISearchBar
Quelques exemples dans Swift
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "", attributes: [NSForegroundColorAttributeName : UIColor.whiteColor()])
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).tintColor = UIColor.whiteColor()
UISearchBar.appearance().setImage(UIImage(named: "searchBarSearchIcon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal)
UISearchBar.appearance().setImage(UIImage(), forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Normal)
tilisez le code ci-dessous pour rendre votre barre de recherche transparente
// Set it to your UISearchBar appearance
[[UISearchBar appearance] setBackgroundColor:[UIColor clearColor]];
[[UISearchBar appearance] setBackgroundImage:backgroundImage];
[[UISearchBar appearance] setSearchFieldBackgroundImage:searchFieldImage
forState:UIControlStateNormal];
Il y a peu de tentatives, y compris la sous-classe UISearchBar et pirater c'est layoutSubviews
ou drawLayer:
. Après iOS 5, la meilleure méthode consiste à utiliser UIAppearance.
// Configure your images
UIImage *backgroundImage = [UIImage imageNamed:@"searchbar"];
UIImage *searchFieldImage = [[UIImage imageNamed:@"searchfield"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
// Set it to your UISearchBar appearance
[[UISearchBar appearance] setBackgroundImage:backgroundImage];
[[UISearchBar appearance] setSearchFieldBackgroundImage:searchFieldImage forState:UIControlStateNormal];
(UITextField.appearance (whenContainedInInstancesOf: [UISearchBar.self])) .defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.white]