Ios UISearchBar在单击时消失

Ios UISearchBar在单击时消失,ios,uisearchbar,uisearchcontroller,Ios,Uisearchbar,Uisearchcontroller,我想使用UISearchController在导航栏下添加一个搜索栏。以下是我所做的: SearchResultsTableViewController *resultsVC = [[SearchResultsTableViewController alloc] init]; self.searchController = [[UISearchController alloc] initWithSearchResultsController:resultsVC]; self.searchCont

我想使用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;
它仍然不起作用。不仅如此,在导航栏离开屏幕后,顶部还有一个黑色的栏。我做错了什么?