如何自定义UISearchBar的外观

我想自定义search栏,我的意思是白色的盒子。 我可以做吗? 有没有关于这方面的文件? 有没有办法隐藏白色的盒子,但不是字母。 我可以至less使箱子变小吗? 我的意思是更less的高度

通过IOS 5.0你有机会使用

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

这是我正在寻找的解决scheme:子类UISearchBar,并覆盖方法layoutSubviews

 - (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; 

以下是一些可以更改以自定义UISearchBar的常用属性: 在这里输入图像说明

你可以改变UISearchBar的背景

自定义一个UISearchBar背景

如何控制UISearchBar背景颜色

这里有很less的尝试,包括子类UISearchBar,并破解它的layoutSubviewsdrawLayer: iOS 5之后,最好的方法是使用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]; 

使用下面的代码来透明你的search栏

 // Set it to your UISearchBar appearance [[UISearchBar appearance] setBackgroundColor:[UIColor clearColor]]; [[UISearchBar appearance] setBackgroundImage:backgroundImage]; [[UISearchBar appearance] setSearchFieldBackgroundImage:searchFieldImage forState:UIControlStateNormal]; 

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)