问题
I'm trying to figure out why my whole navigation bar disappears when I start typing in the UISearchController.searchBar It loads properly and animates properly, but I lose the active NavBar when I start typing. Here's the code to load the searchController from viewDidLoad:
UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
searchResultsController.tableView.backgroundColor = [UIColor redColor];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.delegate = self;
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = YES;
self.searchController.hidesNavigationBarDuringPresentation = NO;
[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = NO;
And here's the result after I start typing:

Notice how the table view is there, but it appears to occupy the space of the nav controller and extend downwards into the first section heading. Any ideas?
Thanks.
回答1:
You need to set definesPresentationContext to YES. From apples example:
// 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
回答2:
this work for me
self.navigationController.navigationBar.translucent = true;
回答3:
So, I was eventually able to solve this problem with this following init sequence. However, I'm not too sure what I did to make this happen. Kind of busy right now, so I'll just paste this solution and will accept a solution if somebody can pinpoint.
- (void)loadSearchBar
{
NSLog(@"Loading SearchBar");
UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
UITableView *myTv = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
searchResultsController.tableView = myTv;
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
searchResultsController.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
searchResultsController.tableView.sectionIndexColor = [UIColor colorWithHexString:linkBlue];
self.searchResultsController = searchResultsController;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.delegate = self;
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = YES;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
self.searchBar = self.searchController.searchBar;
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.searchBar.delegate = self;
self.searchBar.translucent = YES;
self.tableView.tableHeaderView.layer.zPosition++;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
//add color to search field
UIView *subviews = [self.searchBar.subviews lastObject];
UITextField *textView = (id)[subviews.subviews objectAtIndex:1];
textView.backgroundColor = [UIColor colorWithHexString:textFieldBlue];
}
来源:https://stackoverflow.com/questions/26637215/navigation-bar-disappears-when-typing-in-uisearchcontroller-text-field