我遵循了这个指南:http://www.jhof.me/simple-uisearchcontroller-implementation/
我的代码唯一的区别是
而不是self.tableView.tableHeaderView = self.searchController.searchBar;
我有self.navigationItem.titleView = self.searchController.searchBar;
当我从结果表视图控制器中删除时,uisearchbar会保留在导航栏中。
答案 0 :(得分:0)
我从Apple重写样本,您可以看到我发表评论- (void)viewDidLoad {
[super viewDidLoad];
APLResultsTableController *qresultsTableController = [[APLResultsTableController alloc] init];
self.resultsTableController = qresultsTableController;
_searchController = [[UISearchController alloc] initWithSearchResultsController:qresultsTableController];
self.searchController.searchResultsUpdater = self;
[self.searchController.searchBar sizeToFit];
// self.tableView.tableHeaderView = self.searchController.searchBar;
self.navigationItem.titleView = self.searchController.searchBar;
self.searchController.hidesNavigationBarDuringPresentation = NO;
// we want to be the delegate for our filtered table so didSelectRowAtIndexPath is called for both tables
self.resultsTableController.tableView.delegate = self;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES
self.searchController.searchBar.delegate = self; // so we can monitor text changes + others
// Search is now just presenting a view controller. As such, normal view controller
// presentation semantics apply. Namely that presentation will walk up the view controller
// hierarchy until it finds the root view controller or one that defines a presentation context.
//
self.definesPresentationContext = YES; // know where you want UISearchController to be displayed
}
,然后将其设置为导航栏的标题视图,就像您一样。您可以在快照中找到搜索栏。
hidesNavigationBarDuringPresentation
快照在这里,
UISearchBarController有一个名为self.searchController.hidesNavigationBarDuringPresentation = NO;
的属性,默认为YES,如果要将其用作导航项目的标题视图,则将其设置为NO
{{1}}
我测试过,它有效。希望它有所帮助。