自动演示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 :(得分:4)
您正在以错误的方式呈现搜索控制器。看一下文档的一部分。
您可以将搜索控制器与现有视图配合使用 控制器。如果您有一个具有可搜索内容的视图控制器, 将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
之类的地方设置搜索控制器,而不是按下按钮。
您在navigationController
中获得对ArticleSearchViewController
的引用的方式必须更新为:
[self.presentingViewController.navigationController pushViewController:articleDetailController animated:YES];
使用可搜索内容查看控制器:
@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
结果控制器:
@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
答案 1 :(得分:1)
这是一个更好的答案:
definesPresentationContext = true
只需在您的MyViewController