Can background or borders be removed from UISearchBar?

社会主义新天地 提交于 2019-12-04 07:34:59

Try this code,It worked for me

for (id img in yourSearchBar.subviews) {
        if ([img isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
           [img removeFromSuperview];    
        }
    }

In the above code yourSearchBar is the searchbar from which you want to remove the background, And the for loop is searching for the background image of UISearchBar

Scott Sherwood

You should really use the appearance protocol. You can either change it for a single instance of the UISearchBar,

UIImage *searchBg = [UIImage imageNamed:@"search_bar.png"];
[searchBar setBackgroundImage:searchBg];

or if you have multiple search bars across different scenes this will theme every instance (but call it somewhere sensible like the AppDelegate to ensure the bars are all setup correctly before you see any of them.

UIImage *searchBg = [UIImage imageNamed:@"search_bar.png"];
[[UISearchBar appearance] setBackgroundImage:searchBg];

Update: OK, to change the background of a search bar you should use one of the following,

Single instance of searchBar:

[searchBar setSearchFieldBackgroundImage:searchBg forState:UIControlStateNormal];

All instances of UISearchBars:

[[UISearchBar appearance] setSearchFieldBackgroundImage:searchBg forState:UIControlStateNormal];

Hope this helps.

In iOS 7 you can just set:

searchBar.barTintColor = [UIColor clearColor];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!