How do I change the width of the UISearchBar when it is inside a table header so that the section index doesn't overlap the textfield? I want to recreate the same layout you find in the contacts view of the phone app.
+2
A:
One possible solution is to add a custom view to the tableHeaderView. This is what I did:
searchBar is a UISearchBar I declared in the header file. I created a UIView and add a UINavigationBar for the entire width and add the short UISearchBar on top of it. Then, I finally assigned the UIView to tableHeaderView.
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 290.0, 44.0)];
UIView *customTableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
UINavigationBar *dummyNavigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[customTableHeaderView addSubview:dummyNavigationBar];
[customTableHeaderView addSubview:searchBar];
[dummyNavigationBar release];
self.tableView.tableHeaderView = customTableHeaderView;
[customTableHeaderView release];
A more elegant solution can be to stretch the UISearchBar using the contentStretch property. I still have to try that and check if it works.
srik
2010-04-07 10:27:18
Setting **contentStretch** did not work for me.
Sney
2010-04-11 11:44:34
@Snej hmm.. then this is the only solution i know of.. thanks for letting me know :)
srik
2010-04-25 13:28:11
A:
If you use a UISearchDisplayController, the desired behavior will work automagically as noted in this answer.
Scott McCammon
2010-07-01 02:55:08