UISearchController不会在前面但在UISearchController下面推视图控制器

问题描述:

自动演示UISearchController和我自己提供的搜索控​​制器之间的行为不同。UISearchController不会在前面但在UISearchController下面推视图控制器

@implementation MyViewComtroller 

// click search barbutton on right of navigationBar 
- (void)searchAction:(id)sender { 
    ArticleSearchViewController *searchResultsController = [[ArticleSearchViewController alloc] init]; 
    UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController]; 
    searchController.searchResultsUpdater = searchResultsController; 
    searchController.searchBar.delegate = searchResultsController; 
    searchController.delegate = searchResultsController; 
    searchController.hidesNavigationBarDuringPresentation = NO; 
    [self presentViewController:searchController animated:YES completion:nil]; 
} 

@end 

@implementation ArticleSearchViewController 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    ArticleDetailController * articleDetailController = [ArticleDetailController new]; 
    [(UINavigationController *)self.presentingViewController pushViewController:articleDetailController animated:YES]; 
} 

@end 

推动动作发生在UISearchController下面。

+0

如果为SearchResultsControl设置了一个UINavigationController ler,推动动作发生在UISearchController上,但searchBar不能隐藏动画。 – weiminghuaa

您以错误的方式呈现搜索控制器。看看部分文档。

您可以使用搜索控制器与您现有的视图 控制器相结合。当你有一个带有可搜索内容的视图控制器时, 将UISearchController对象的搜索栏合并到你的视图控制器的界面中。当用户与该搜索栏进行交互时,搜索控制器会自动在您指定的搜索结果中显示新的视图控制器 。

[self presentViewController:searchController animated:YES completion:nil]; 

你应该这样做iOS上的11:

self.definesPresentationContext = YES; 
self.navigationItem.searchController = searchController; 

还是这个预iOS的11:

self.definesPresentationContext = YES; 
self.navigationItem.titleView = searchController.searchBar; 

在主控制器

取而代之的是或放置在您展示的其他地方searchController.searchBar视图层次。

您还应该在viewDidLoad这样的地方设置搜索控制器,而不是按下按钮。

而且你得到一个关于你navigationControllerArticleSearchViewController必须进行更新,以类似的方式:

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self.view setBackgroundColor:[UIColor blackColor]]; 

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
    ArticleSearchViewController *searchResultsController = [sb instantiateViewControllerWithIdentifier:@"ArticleSearchViewController"]; 
    UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController]; 
    searchController.searchResultsUpdater = searchResultsController; 
    searchController.searchBar.delegate = searchResultsController; 
    searchController.delegate = searchResultsController; 
    searchController.hidesNavigationBarDuringPresentation = NO; 
// [self presentViewController:searchController animated:YES completion:nil]; //WRONG 
    self.definesPresentationContext = YES; 
    self.navigationItem.searchController = searchController; 
} 

@end 

结果控制器:与搜索内容

[self.presentingViewController.navigationController pushViewController:articleDetailController animated:YES]; 

视图控制器

@implementation ArticleSearchViewController 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    UIViewController * articleDetailController = [UIViewController new]; 
    [self.presentingViewController.navigationController pushViewController:articleDetailController animated:YES]; 
} 

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController { 
    // update appropriately 
} 

@end