Lorenzo B
Lorenzo B

Reputation: 33428

Modify UISearchBar width in a UITableView header

I'm running the following snippet of code (in ViewDidLoad method of a UITableViewController) to create a UISearchBar and add it to a UITableView header.

UISearchBar search = new UISearchBar();
search.SizeToFit (); // How to modify width?
TableView.TableHeaderView = searchBar;

Do you if there is a workaround to modify the width of the UISearchBar. The previous snippet of code allows the bar to have a width as the screen one. Giving a frame to the bar seems not working.

Thank you in advance.

EDIT: The sample has been created for MonoTouch, but it can apply for iOS in general.

Upvotes: 0

Views: 1094

Answers (1)

Lorenzo B
Lorenzo B

Reputation: 33428

This is the solution that I found:

  • create a UIToolbar
  • add the UISearchBar to that toolbar
  • add the toolbar to the header of a UITableView

Since in a UIToolbar it is possible to insert space elements (i.e. UIBarButtonItemFlexibleSpace), the width of a search bar can be modified easily.

Upvotes: 1

Related Questions