我想在UISearchController的导航栏下添加一个搜索栏。这是我做的:
SearchResultsTableViewController *resultsVC = [[SearchResultsTableViewController alloc] init];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:resultsVC];
self.searchController.searchResultsUpdater = self;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.dimsBackgroundDuringPresentation = YES;
UISearchBar *searchBar = self.searchController.searchBar;
[searchBar sizeToFit];
searchBar.placeholder = @"Search";
self.definesPresentationContext = YES;
[self.view addSubview:searchBar];
searchBar.translatesAutoresizingMaskIntoConstraints = NO;
[NSLayoutConstraint activateConstraints:@[
[searchBar.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor],
[searchBar.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
[searchBar.widthAnchor constraintEqualToAnchor:self.view.widthAnchor],
[searchBar.heightAnchor constraintGreaterThanOrEqualToConstant:0]
]];
但是,每当我点击该栏时,它就会消失。 如果我使用以下内容隐藏导航栏:
self.searchController.hidesNavigationBarDuringPresentation = YES;