UISearchBar在点击时消失

时间:2017-08-19 15:57:47

标签: ios uisearchbar uisearchcontroller

我想在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;

它仍然无效。不仅如此,导航栏离开屏幕后,顶部还有一个黑条。我在这做错了什么? enter image description here

0 个答案:

没有答案