wei*_*uaa 2 uinavigationbar uinavigationcontroller uisearchbar ios 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
Run Code Online (Sandbox Code Playgroud)
推送操作发生在UISearchController下面。
您以错误的方式显示搜索控制器。查看部分文档。
您可以将搜索控制器与现有视图控制器一起使用。当您的视图控制器具有可搜索的内容时,请将UISearchController对象的搜索栏合并到视图控制器的界面中。当用户与该搜索栏交互时,搜索控制器会自动显示一个新的视图控制器以及您指定的搜索结果。
而不是在您的主控制器中:
[self presentViewController:searchController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
您应该在iOS 11上执行此操作:
self.definesPresentationContext = YES;
self.navigationItem.searchController = searchController;
Run Code Online (Sandbox Code Playgroud)
或者在iOS 11之前的版本中:
self.definesPresentationContext = YES;
self.navigationItem.titleView = searchController.searchBar;
Run Code Online (Sandbox Code Playgroud)
或searchController.searchBar在显示的视图层次结构中放置其他任何位置。
您还应该将搜索控制器设置在类似viewDidLoad而不是按下按钮的地方。
而你得到你一个参考的方式navigationController中ArticleSearchViewController必须进行更新,以这样的:
[self.presentingViewController.navigationController pushViewController:articleDetailController animated:YES];
Run Code Online (Sandbox Code Playgroud)
具有可搜索内容的View Controller:
@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
Run Code Online (Sandbox Code Playgroud)
结果控制器:
@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
Run Code Online (Sandbox Code Playgroud)