gpt4 book ai didi

ios - 如何在 UINavigationBar 中隐藏 UISearchController

转载 作者:可可西里 更新时间:2023-11-01 05:32:52 26 4
gpt4 key购买 nike

我有一个导航栏,当屏幕首次显示时,它上面没有搜索栏。我触发并在导航栏中显示搜索栏,但我想知道如何隐藏搜索栏并再次显示导航栏标题。

我按如下方式创建搜索栏:

_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];

self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.placeholder = nil;
[self.searchController.searchBar sizeToFit];
//self.tableView.tableHeaderView = self.searchController.searchBar;
self.sharedNavigationItem.titleView = _searchController.searchBar;

self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES
self.searchController.searchBar.delegate = self; // so we can monitor text changes + others
self.definesPresentationContext = YES;
_searchController.hidesNavigationBarDuringPresentation = NO;

我只想在这里隐藏搜索栏并显示带标题的普通导航栏:

enter image description here

最佳答案

我遇到了同样的问题。以前的答案建议废弃整个 NavigationBar,但这不是研究的行为。解决方案是在按下搜索按钮时添加 searchBar...

@IBAction func didPressAddButton(_ sender: Any) {
if #available(iOS 11.0, *) {
// For iOS 11 and later, place the search bar in the navigation bar.
navigationItem.searchController = tagSearchController
navigationItem.hidesSearchBarWhenScrolling = true
} else {
// For iOS 10 and earlier, place the search controller's search bar in the table view's header.
tableView.tableHeaderView = tagSearchController.searchBar
}

tagSearchController.isActive = true
}

...并在调用 didDismissSearchController 时将其删除(UISearchControllerDelegate 方法)。

func didDismissSearchController(_ searchController: UISearchController) {
if #available(iOS 11.0, *) {
// For iOS 11 and later, place the search bar in the navigation bar.
navigationItem.searchController = nil

} else {
// For iOS 10 and earlier, place the search controller's search bar in the table view's header.
tableView.tableHeaderView = nil
}
}

注意:我的 searchController 是在 viewDidLoad() 中创建和配置的

关于ios - 如何在 UINavigationBar 中隐藏 UISearchController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42704202/

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