如何在iPhone应用程序中自定义搜索栏?

问题描述:

我正在研究搜索栏定制。我搜索了它,并没有找到我的问题的答案。如何在iPhone应用程序中自定义搜索栏?

Search your Match

我用下面的代码来更改背景图片:

searchBar.backgroundColor=[UIColor clearColor]; 
[[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"search-.png"] forState:UIControlStateNormal]; 

[[UISearchBar appearance] setImage:[UIImage imageNamed:@"search-magnifier"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; 

我已经用于获取搜索栏的子视图的代码,但它并没有帮助。有没有人有什么建议?

+1

我注意到你有一个更名图像...是search-.png应该被命名为? – user2277872

+0

感谢您的回复 –

我认为你应该给文件扩展名如user2277872的说法。

+0

好的,谢谢你的回复 –

嗨,我使用下面的代码做了我的搜索栏问题。它的工作正常。

searchBar.layer.masksToBounds=YES; 

searchBar.layer.cornerRadius=5; 
searchBar.backgroundColor=[UIColor colorWithRed:71/255.f green:71/255.f blue:67/255.f alpha:1]; 
searchBar.backgroundImage=[UIImage imageNamed:@"search-block.png"]; 
UITextField *searchField; 
NSUInteger numViews = [searchBar.subviews count]; 
for(int i = 0; i < numViews; i++) 
{ 
    if([[searchBar.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) 
    { //conform? 
     searchField = [searchBar.subviews objectAtIndex:i]; 
    } 
} 
if(!(searchField == nil)) 
{ 
    searchField.textColor = [UIColor whiteColor]; 
    [searchField setBackground: [UIImage imageNamed:@"search-block.png"]];//just add here gray image which you display in quetion 
    [searchField setBorderStyle:UITextBorderStyleNone]; 
} 
[[UISearchBar appearance] setImage:[UIImage imageNamed:@"search-magnifier"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];