在navigationBar中使用searchBar时需要显示导航标题

问题描述

在使用searchBar进行搜索时,我需要显示较大的导航标题,但是当我单击Searchbar或开始键入时,我的NavigationTitle会替换为SearchBar。我检查了navigationItem的方法,但没有找到合适的方法

有人知道我们即使在搜索时也能始终显示标题吗? 这是我的代码

    class FavouriteVC: UIViewController {
        let searchController = UISearchController(searchResultsController: nil)
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            view.backgroundColor = .white
            
            navigationController?.navigationBar.prefersLargeTitles = true
            navigationItem.title = "Favourite"
            navigationItem.searchController = searchController
            navigationItem.largeTitledisplayMode = .always
        }
    }

这是镜头 screen with NavTitle screen without NavTitle

解决方法

好像UISearchController有一个hidesNavigationBarDuringPresentation可以防止将导航栏设置为false时隐藏

包括:

searchController.hidesNavigationBarDuringPresentation = false

在您的viewDidLoad中,如果我理解您的问题是对的,这应该可以满足您的要求。