1

Disclaimer: I'm targeting iOS 7. I realize UISearchDisplayController is deprecated in iOS8, but it's what I need to use.

I have the standard navigation bar hidden. Below a UIView, I have a UISearchBar and a UITableView. It looks like this:

enter image description here

When I attempt to search, the results filter fine, but the searchResultsTableView flies to the top of the screen.

enter image description here

The UISearchBar remains in the same place, but the tableView moves to cover the entire screen.

I'm trying to prevent the tableView from moving. The desired behavior is to have both the UISearchBar and the searchResultsTableView remain in place

What I've tried so far:

  1. I looked through the delegates for searching to see if this could be easily disabled and couldn't find anything.
  2. I'm using Autolayout so setting a constraint programmatically to move the top of the searchResultTableView would be logical.

(please excuse the formatting)

-(void) searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView {

 NSDictionary * views = @{ @"searchResultsTableView" : self.searchDisplayController.searchResultsTableView};

NSArray * topConstraint = [NSLayoutConstraint
                           constraintsWithVisualFormat:@"V:|-64-[searchResultsTableView]-|"
                           options:0
                           metrics:nil
                           views:views];
[self.searchDisplayController.searchResultsTableView.superview addConstraints:topConstraint]; }

I also tried adding the constraint into "willShowSearchResultsTableView" I'm not familiar with adding constraints programmatically so that might be the source of the issue.

  1. I know that I can fix the issue by placing the TableView and SearchBar inside a ContainerView, but I'm trying to avoid doing that since that's superfluous and wasteful.

Thanks in advance everyone!

1 Answer 1

0

It turns out answer number two was correct. I found in the debugger logs that NSAutoresizingMaskLayoutConstraint was breaking the NSLayoutConstraint that I was trying to impose on the searchResultsTableView.

   self.searchDisplayController.searchResultsTableView.translatesAutoresizingMaskIntoConstraints = NO;

This line fixes the issue by turning off the mask into constraints direct conversion. I put that in viewDidLoad and then my own custom constraint was followed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.