Setting translucent to NO on UISearchBar

后端 未结 2 886
粉色の甜心
粉色の甜心 2021-01-19 02:27

We have a UITableView with a searchbar added with the searchDisplayController.

We want to have translucency off throughout th

2条回答
  •  野的像风
    2021-01-19 03:00

    For UISearchBarStyleProminent:

    1) Be sure to check the "Translucent" box for the search bar in the Attributes Inspector.

    2) Add the following to viewDidLoad:

    self.navigationController.navigationBar.translucent = NO; // If you have a navBar
    self.searchDisplayController.searchBar.translucent = NO;
    

    Edit From @RudolfAdamkovic:

    "I've found that for UISearchBarStyleProminent, executing [the following] helps. That way, you can keep it on in Storyboard."
    searchBar.translucent = YES;
    searchBar.translucent = NO;

    For UISearchBarStyleMinimal:

    In order to get the minimal search bar to not be translucent I have put together a workaround.

    1) Be sure to check the "Translucent" box for the search bar in the Attributes Inspector.

    2) Add the following code to viewDidLoad:

    self.navigationController.navigationBar.translucent = NO;
    self.searchDisplayController.searchBar.translucent = NO;
    self.searchDisplayController.searchBar.backgroundColor = [UIColor desiredColor];
    

    3) A UIView needs to be added to the viewController. This view needs to be 20px heigh and should have the same color as the searchBar.barTintColor.

    Note: I think this workaround is needed because: "The style UISearchBarStyleMinimal provides no default background color or image but will display one if customized as such." Thus the only way to get this functionality for UISearchBarStyleMinimal is to set the backgroundColor.

    See UISearchBar documentation for more details.

提交回复
热议问题