如何在UINavigationBar中隐藏UISearchController

6
我是一名有用的助手,可以为您进行文本翻译。以下是需要翻译的内容:

当屏幕首次显示时,我的导航栏中没有搜索栏。我通过触发并显示导航栏中的搜索栏,但现在想知道如何隐藏搜索栏并再次显示导航栏标题。

我创建搜索栏如下:

_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


你尝试过再次更改self.sharedNavigationItem.titleView吗? - Teja Nandamuri
你找到解决方案了吗?我现在也遇到了同样的问题。 - Tommy Sadiq Hinrichsen
3个回答

3

我曾经遇到过同样的问题。之前的答案建议将整个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()` 中创建和配置的。

2

这会隐藏整个导航栏。我只想隐藏搜索栏并显示普通的导航栏。 - birdcage

2

如果您想隐藏整个导航栏并且要使用自己的导航栏,首先可以隐藏导航控制器的导航栏。

[self.navigationController setNavigationBarHidden:YES];
//OR
[self.navigationController setNavigationBarHidden:YES animated:NO];

或者,如果您已经为导航栏使用了自定义标题视图,则可以执行以下操作:
self.navigationItem.titleView.hidden = YES;

如果您想隐藏后退按钮项,可以这样做:
self.navigationItem.hidesBackButton = TRUE;

或者,如果您只想隐藏标题,可以执行以下操作:
self.navigationItem.title = @"";

And

self.navigationController.navigationItem.titleView = nil ;

不,我只想隐藏搜索栏并显示普通的导航栏。 - birdcage
在你的代码中,你在 titleView 中添加了搜索栏,然后尝试使用 self.navigationItem.titleView.hidden = YES; 隐藏它,接着使用 self.navigationController.navigationItem.titleView = nil ; - Sid Mhatre
它没有帮助。 - birdcage
它是否隐藏搜索栏? - Sid Mhatre
它没有影响任何事情。 - birdcage
self.sharedNavigationItem.titleView.hidden = YES; 然后 self.sharedNavigationItem.navigationItem.titleView = nil ; 解决了。谢谢。 - birdcage

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接