Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView section index overlaps search bar

Hi: I want to display a section index in an UITableView with a search bar in the table view header (not section header). But the index strip is now overlapping the search bar. Is there an elegant solution to avoid this and let the index start below the table header?

like image 852
Sney Avatar asked Apr 11 '10 11:04

Sney


2 Answers

I realize this answer comes very late, but I had the same problem and searched here and elsewhere. I found good answers here: Changing the size of the UISearchBar TextField?

There are several suggestions, but the one I found most straightforward was to put a UISearchBar and UINavBar in a UIView and pass that view to setTableView. The problem is that the table controls the size of the header view, ignoring any setting you add. So adding the view makes the table happy and you can reach inside and set the search bar size without being interfered with by the table.The UINavBar has the same background styles as the UISearchBar, so fills the extra space in a visually consistent way, though you have to tweak its frame - See below:

UISearchBar searchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0, 1, 290, 40)] autorelease];
UIView *searchView = [[[UIView alloc] initWithFrame:CGRectMake(0, 1, 320, 40)] autorelease];
UINavigationBar *navBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(1, -1, 320, 41)] autorelease]; // same gradient/style as search bar
[navBar setTintColor:[UIColor lightGrayColor]];
[searchView addSubview:navBar];
[searchView addSubview:searchBar];
like image 117
Jon Avatar answered Oct 10 '22 10:10

Jon


I add empty strings to indexes array:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSMutableArray *array = [NSMutableArray arrayWithArray:_sortedKeys];

    [array insertObject:@"" atIndex:0];
    [array insertObject:@"" atIndex:0];

    return array;
}
like image 40
Andrew Avatar answered Oct 10 '22 11:10

Andrew