Sney
Sney

Reputation: 2506

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?

Upvotes: 10

Views: 8731

Answers (2)

Andrew
Andrew

Reputation: 49

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;
}

Upvotes: 4

Jon
Jon

Reputation: 472

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];

Upvotes: 10

Related Questions