gpt4 book ai didi

ios - UISearchController 不要将 viewcontroller 推到前面而是在 UISearchController 下面

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:05:40 25 4
gpt4 key购买 nike

自动呈现 UISearchController 和自己呈现搜索 Controller 的行为不同。

@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 下。

最佳答案

您以错误的方式呈现搜索 Controller 。查看部分文档。

You use a search controller in tandem with your existing view controllers. When you have a view controller with searchable content, incorporate the search bar of a UISearchController object into your view controller’s interface. When the user interacts with that search bar, the search controller automatically displays a new view controller with the search results that you specify.

而不是在你的主 Controller 中:

[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 放置在您呈现的 View 层次结构中的任何其他位置。

您还应该在 viewDidLoad 之类的地方设置搜索 Controller ,而不是在按下按钮时。

并且您在 ArticleSearchViewController 中获取对 navigationController 的引用的方式必须更新为如下内容:

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

带有可搜索内容的 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

结果 Controller :

@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

关于ios - UISearchController 不要将 viewcontroller 推到前面而是在 UISearchController 下面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42855378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com