web-dev-qa-db-fra.com

Comment personnaliser l'apparence de UISearchBar

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

39
Oscar

Par IOS 5.0 vous avez la possibilité d'utiliser

  [[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"searchbar.png"]forState:UIControlStateNormal];
57
Ilker Baltaci

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];
}
28
Oscar

Voici quelques propriétés communes que vous pouvez modifier pour personnaliser UISearchBar: enter image description here

16
wj2061
// 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;
15
Kirit Vaghela
7
Jhaliya

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)
2
Maselko

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];
1
Waqas Haider Sheikh

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];
1
James Tang

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

0
user11130801