Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchController: Section Index of UITableView overlaps searchBar

I'm building an iOS 8 app that uses the new UISearchController. In the table view related to the search controller, I'm using a section index to let users jump quickly from one section of the table to the next. It's working just fine, but the section index overlaps the search bar on the table / search controller. Has anyone run into this issue before and, if so, how did you solve it? Below is how I'm initializing my search controller:

self.resultsTableController = [self.storyboard instantiateViewControllerWithIdentifier:[SelectSpecialtySearchResultsTVC storyboardId]];
UINavigationController *searchResultsNavController = [[UINavigationController alloc]initWithRootViewController:self.resultsTableController];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsNavController];

self.searchController.searchResultsUpdater = self;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.barTintColor = [UIColor colorWithHexString:kColorGrayLight];
self.searchController.searchBar.translucent = NO;
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, [self.view bounds].size.width, 44.0);
self.searchController.searchBar.delegate = self;

self.tableView.tableHeaderView = self.searchController.searchBar;

//present the search display controller within the confines of this class's table view
self.definesPresentationContext = YES;

// we want to be the delegate for our filtered table so didSelectRowAtIndexPath is called for both tables
self.resultsTableController.tableView.delegate = self;
self.searchController.delegate = self;

enter image description here

like image 249
Brian Sachetta Avatar asked Mar 06 '15 19:03

Brian Sachetta


2 Answers

just change background color of sectionIndex:

Swift:

    tableView.sectionIndexBackgroundColor = UIColor.clearColor()
like image 189
rgreso Avatar answered Sep 19 '22 15:09

rgreso


I know this is an old question, but I've seen this come up a couple of times.. Instead of the above solutions the easier answer is to create a new UIView with the original bounds and then iOS does not shrink the width to accommodate the table index.

I.e. in viewDidLoad

let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: searchController.searchBar.bounds.size.height))
headerView.addSubview(searchController.searchBar)
tableView.tableHeaderView = headerView
like image 21
mm282 Avatar answered Sep 21 '22 15:09

mm282