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

时间:2013-09-21 14:25:15

标签: iphone ios5 ios4 ios6.0

我正在进行搜索栏自定义。我用Google搜索了,我没有找到问题的答案。

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

我已经使用代码获取搜索栏的子视图,但它没有帮助。有没有人有任何建议?

2 个答案:

答案 0 :(得分:1)

我认为您应该提供文件扩展名,如user2277872的说法。

答案 1 :(得分: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];