Search code examples
iphoneuitableviewuisearchbaruisearchdisplaycontroller

Resize search bar in search display controller when section index titles are removed


I have a search display controller in a table view. In this table view, I will sometimes display section index titles but the contents of the table view can be changed and sometimes I will hide the section index titles. When the table view first loads, the section index titles are shown and the search bar has resized to make room for the section index titles. If the contents of the table view changes so that the section index titles are removed, the search bar will not change its size. This will leave an empty space to the right of the search bar. Then, if you start searching but cancel it will resize to use the room where the section index titles used to be and if the content of the tabled is changed again and the section index titles are shown, the search bar will change its size to make room for the titles.

Is there any way to update the size of the search bar when the section index titles are removed?

EDIT

I have tried setting the frame of the searchBar in searchDisplayController but this does not have any effect. E.g.

CGRect frame = self.searchDisplayController.searchBar.frame;
frame.size.width = 300;
self.searchDisplayController.searchBar.frame = frame;

The code above will not resize the search bar.


Solution

  • Rather than add the search bar directly to the table view, add a view to the table view and then add the search bar to that. This will allow you to resize the search bar.