lazy var searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width:
self.view.bounds.width - 50, height: 30))
searchBar.delegate = self
searchBar.sizeToFit()
navigationBar.topItem?.titleView = searchBar
Storyboard设计中的导航栏具有搜索栏,该搜索栏在按下 viewController 时隐藏, Storyboard设计中的导航栏在呈现 VC 时显示。在推送 VC 时,将相同的搜索栏设置为
self.navigationItem.titleView = searchBar
也试过了,
navigationController?.navigationBar.topItem?.titleView = searchBar
titleView 在 viewDidLoad() 和 viewDidAppear() 上显示,但是当进入后台并返回到应用程序时,搜索栏消失。
最佳答案
为 UISearchBar
使用 UISearchController
。喜欢,
class ViewController: UITableViewController, UISearchResultsUpdating {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
self.definesPresentationContext = true
// Place the search bar in the navigation item's title view.
self.navigationItem.titleView = searchController.searchBar
// Don't hide the navigation bar because the search bar is in it.
searchController.hidesNavigationBarDuringPresentation = false
}
}
您可以获得更多信息here .
希望对您有所帮助。
关于ios - 当应用程序进入后台时,navigationItem 的 titleView 中的 searchBar 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59050174/