iPad/iPhone uiSearchbar transparent background

旧时模样 提交于 2019-12-18 09:35:55

问题


I know this questions was asked(and solved) before, but this wont work for me. as a matter of fact, I had it already solved, but this issue came back out of nowhere and struck me on the head.

I am not able to set the background of my UISearchBar transparent. I was always using:

searchBar.backgroundColor = [UIColor clearColor];   
[[searchBar.subviews objectAtIndex:0] removeFromSuperview];

and it worked nicely... but suddenly it stopped. could be since I upgraded my xcode-version but I am not sure. I spent a couple of hours already investigating this.. Is somebody out there to do this? please point me in the right direction. thanks heaps!!!

best regards T


回答1:


Try looping through your subviews and look for the right class:

for (UIView *subview in searchBar.subviews) {
    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
        [subview removeFromSuperview];
        break;
    }
}

I'm not sure but I dont think you can assume that index 0 is the background view. The example above works for me.




回答2:


It still works for me, so maybe something else in your code messes it up. Are you sure searchBar is properly set up (connected outlet in IB, the call is called after the view is initialized, etc...)? Best way is to just print out the content of [searchBar.subviews objectAtIndex:0], it should be a UISearchBarBackground object. And btw, since Apple can change the UISearchBar view hierarchy anytime they want, you better make some check: searchBar.subviews.count, and

if ([[searchBar.subviews objectAtIndex:0] class] == NSClassFromString(@"UISearchBarBackground")) ...

before calling that.




回答3:


You just need to change the searchBarStyle property to UISearchBarStyleMinimal. By default it is UISearchBarStyleProminent. You can change it in storyboard or in your code as shown below.

self.yourSearBar.searchBarStyle = UISearchBarStyleMinimal;


来源:https://stackoverflow.com/questions/4943994/ipad-iphone-uisearchbar-transparent-background

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