UISearchController search bar overlaps first cell when active

I use UISearchController to search for data in my table view. I do not use a table view controller. I would like to hide the navigation bar when the search bar is active, so I set self.searchController.hidesNavigationBarDuringPresentation = YES; in yes.

However, when I do this and want to do a search, my active search bar covers part of the first cell. the covered part has the same height as the status bar.

active status bar

I tried other articles and questions like this, but nothing helped me solve it. Then I started playing with the size of the table title. I did it

  -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 20.0f; } 

As a result, I came across a search bar, and there were no more problems starting the search.

search controller with header size 20

However, when the search bar is not active, between the search bar and the first cell

enter image description here

Any ideas on how to fix this?

Edit: after adding self.automaticallyAdjustsScrollViewInsets = false;

enter image description here

+1
ios objective-c iphone uitableview
Jul 05 '17 at 15:01
source share
6 answers

This is how I set up the search bar and things in viewDidLoad (copied from some examples from Apple).

It displays the results found in the same view controller as your unfiltered data. It also has its own search bar in the table header, which is hidden until it is needed.

 self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; self.searchController.searchResultsUpdater = self; self.tableView.tableHeaderView = self.searchController.searchBar; [self.searchController.searchBar sizeToFit]; // we want to be the delegate for our filtered table so didSelectRowAtIndexPath is called for both tables self.searchController.delegate = self; self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES self.searchController.searchBar.delegate = self; // so we can monitor text changes + others // Search is now just presenting a view controller. As such, normal view controller // presentation semantics apply. Namely that presentation will walk up the view controller // hierarchy until it finds the root view controller or one that defines a presentation context. // self.definesPresentationContext = YES; // know where you want UISearchController to be displayed // Hides search bar initially. When the user pulls down on the list, the search bar is revealed. [self.tableView setContentOffset:CGPointMake(0, self.searchController.searchBar.frame.size.height)]; 
+3
Jul 05 '17 at 19:05
source share
— -

I managed to solve this problem by combining RJiryes answer with the list up.

 -(void)willPresentSearchController:(UISearchController *)searchController{ [self.contactsTableView setContentInset:UIEdgeInsetsMake(20, 0, 0, 0)]; [self.contactsTableView setContentOffset:CGPointMake(0.0f, -self.contactsTableView.contentInset.top) animated:YES]; } -(void)willDismissSearchController:(UISearchController *)searchController{ [self.contactsTableView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)]; } 
+6
Jul 05 '17 at 17:04 on
source share

You can add a content attachment at the top rather than add a title.

  self.tableView.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0) 

and when theDISISISSearchController method (UISearchController delegation method) is called, return the inserts to 0

  self.tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) 

This way you avoid spaces if they are not active.

+3
Jul 05 '17 at 15:16
source share

Not the best solution, but only to solve the problem.

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return searchController.isActive ? 20.0f : 0.0f; }

0
Jul 05 '17 at 16:12
source share

I have seen similar behavior. Most likely your table view controller is not using heightForHeaderInSection. The exact cell height is unknown, which leads to a number of problems like yours.

0
Jul 05 '17 at 16:25
source share

I have the same problem. And studies have not responded. My decision:

1) I used the UITableviewController with SearchController, and when I clicked on the field, I had a problem with the user interface: Extra empty space between SearchBar and Tableview The guys noted that you need to use the searchController with the UIViewcontroller and tableView separately. Therefore i did

2) There was this problem. It is solved as follows:

  let searchController = UISearchController(searchResultsController: nil) var tableView:UITableView! override func viewDidLoad() { super.viewDidLoad() loadData() setupTableView() setupSearchController() } private func setupTableView(){ tableView = UITableView(frame: CGRect.zero, style: .plain) **//next 2 very important line of code** tableView.autoresizingMask = UIViewAutoresizing() tableView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(tableView) tableView.dataSource = self tableView.delegate = self tableView.register(LetterBrandCell.self, forCellReuseIdentifier: "brandCell") tableView.tableFooterView = UIView() addConstraints() } 

In the method of adding constraints, it is important to be tied to the top and bottom list of LayoutGuides, and not just for viewing:

 private func addConstraints(){ NSLayoutConstraint(item: tableView, attribute: .top, relatedBy: .equal, toItem: topLayoutGuide, attribute: .bottom, multiplier: 1, constant: 0).isActive = true NSLayoutConstraint(item: tableView, attribute: .bottom, relatedBy: .equal, toItem: bottomLayoutGuide, attribute: .top, multiplier: 1, constant: 0).isActive = true NSLayoutConstraint(item: view, attribute: .leading, relatedBy: .equal, toItem: tableView, attribute: .leading, multiplier: 1, constant: 0).isActive = true NSLayoutConstraint(item: view, attribute: .trailing, relatedBy: .equal, toItem: tableView, attribute: .trailing, multiplier: 1, constant: 0).isActive = true } 
-one
Sep 11 '17 at 8:15
source share



All Articles