从搜索结果表 (UISearchController) 导航到视图(推送)并保留搜索结果 [英] Navigating to a view (push) from a search results table (UISearchController) and keeping the search results in place

查看:71
本文介绍了从搜索结果表 (UISearchController) 导航到视图(推送)并保留搜索结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索结果表 (UISearchController),它显示在导航堆栈上的视图上.我在视图中显示搜索控制器和表格,如下所示:

 _searchResultsTableViewController = [[CalendarSearchResultsTableViewController alloc] init];_searchResultsTableViewController.delegate = self;_searchController = [[UISearchController alloc] initWithSearchResultsController:_searchResultsTableViewController];_searchController.delegate = self;_searchController.hidesNavigationBarDuringPresentation = NO;_searchController.dimsBackgroundDuringPresentation = YES;_searchController.obscuresBackgroundDuringPresentation = YES;_searchController.active = 是;_searchController.searchBar.delegate = self;UIEdgeInsets adjustForTabbarInsets = UIEdgeInsetsMake(0, 0, CGRectGetHeight(self.tabBarController.tabBar.frame), 0);_searchResultsTableViewController.tableView.contentInset = adjustForTabbarInsets;_searchResultsTableViewController.tableView.scrollIndicatorInsets = adjustForTabbarInsets;[self.navigationController presentViewController:_searchController 动画:YES 完成:nil];

这会在 UINavigationBar 上显示一个 UISearchBar 并且搜索表按预期显示.当用户从表中选择一个搜索结果时,我想通过将它推到导航堆栈上来显示该选择的视图,同时搜索结果保持原位.我现在的工作方式是显示搜索控制器的视图打开视图,但这需要关闭搜索控制器,因为 search result view 显示在搜索结果表的后面.

如何才能让搜索控制器将搜索结果视图推送到导航堆栈上,允许用户导航回搜索结果?>

可以在日历应用程序的 iOS 邮件中找到我希望它如何工作的示例(当您搜索时).

我在搜索结果控制器中的 navigationController 属性为零,因此我目前无法从那里显示 搜索结果视图.

该项目在 Objective-C 和 Swift 中都使用,所以在任何一个中的答案都很好.非常感谢任何帮助!

解决方案

我认为您需要进行 3 个小改动.

  1. _searchController.hidesNavigationBarDuringPresentation = YES;
  2. [self presentViewController:_searchController animation:YES completion:nil];
  3. self.definesPresentationContext = YES;//在呈现 searchController 的 viewController 的 viewDidLoad

I have a search results table (UISearchController) that is presented on a view that exists on a navigation stack. I present the search controller and table on my view like this:

    _searchResultsTableViewController = [[CalendarSearchResultsTableViewController alloc] init];
_searchResultsTableViewController.delegate = self;

_searchController = [[UISearchController alloc] initWithSearchResultsController:_searchResultsTableViewController];
_searchController.delegate = self;
_searchController.hidesNavigationBarDuringPresentation = NO;
_searchController.dimsBackgroundDuringPresentation = YES;
_searchController.obscuresBackgroundDuringPresentation = YES;
_searchController.active = YES;

_searchController.searchBar.delegate = self;

UIEdgeInsets adjustForTabbarInsets = UIEdgeInsetsMake(0, 0, CGRectGetHeight(self.tabBarController.tabBar.frame), 0);
_searchResultsTableViewController.tableView.contentInset = adjustForTabbarInsets;
_searchResultsTableViewController.tableView.scrollIndicatorInsets = adjustForTabbarInsets;

[self.navigationController presentViewController:_searchController animated:YES completion:nil];

This presents a UISearchBar over the UINavigationBar and the search table appears as expected. When the user selects a search result from the table, I want to present a view for the selection by pushing it onto the navigation stack while the search results remain in place. The way I have it working now is that the view that presented the search controller opens the view, but this requires the search controller to be dismissed because the search result view is presented behind the search results table.

How can I make it so that the search controller pushes the search result view onto the navigation stack, allowing the user to navigate back to the search results?

An example of how I expect this to work can be found in the iOS Mail of Calendar app (when you search).

My navigationController property in the search results controller is nil, so I cannot currently present the search result view from there.

The project is in both Objective-C and Swift, so answers in either is fine. Any help is much appreciated!

解决方案

I think there are 3 small changes you need to make.

  1. _searchController.hidesNavigationBarDuringPresentation = YES;
  2. [self presentViewController:_searchController animated:YES completion:nil];
  3. self.definesPresentationContext = YES; // in viewDidLoad of the viewController that presented the searchController

这篇关于从搜索结果表 (UISearchController) 导航到视图(推送)并保留搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆