How should I present a UISearchController using a

2019-06-16 07:18发布

问题:

Using a universal storyboard with an adaptive UISplitViewController user interface.

I want to present a search controller on the primary (master) side, using the following code (from the master view controller):

static NSString * const kCGISearchViewControllerID = @"SearchViewControllerID";

- (IBAction)searchButtonClicked:(UIBarButtonItem *)__unused sender {
    SearchViewController *searchResultsController = [self.storyboard instantiateViewControllerWithIdentifier:kCGISearchViewControllerID];
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
    self.searchController.searchResultsUpdater = searchResultsController;
    self.searchController.hidesNavigationBarDuringPresentation = NO;
    [self presentViewController:self.searchController animated:YES completion:nil];
}

It initially appears to work correctly (regardless of the starting orientation):

Problems show up after autorotation (keyboard still visible, underlying content still dimmed, but the search bar has disappeared):

Upon rotating back to landscape, the search bar reappears, but its width is now wrong:

(I've also tried placing the search controller's searchBar in the titleView. While the searchBar adapts correctly, the search results controller still doesn't look right.)

What am I missing to get a presented UISearchController to animate itself properly as the UI adapts to changing size classes?

Update:

Adding self.definesPresentationContext = YES; gets the search bar/results to appear within the primary view, but the search bar is animating under that navigation bar, and isn't visible. The other issue is that the search bar height doesn't shrink, when it rotates from portrait (which has a status bar), back to landscape.

回答1:

What Xcode version are you using? What iOS version on the simulator?
Tried that using Xcode 6, iOS 8.4 - That's all the code I used in the MasterVC:

class MasterViewController: UITableViewController {

    @IBAction func search(sender: UIBarButtonItem) {
        let searchController = UISearchController(searchResultsController: nil)
        searchController.hidesNavigationBarDuringPresentation = false
        presentViewController(searchController, animated: true, completion: nil)
    }
...
}

It's presented within the Master and locks the orientation of the screen! The behavior might have changed since you posted your answer.