ios - 当应用程序进入后台时,navigationItem 的 titleView 中的 searchBar 消失
问题描述
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
故事板设计中的导航栏有搜索栏,在推送viewController时隐藏,故事板设计上的导航栏在展示VC时显示。在推送 VC 时,将相同的搜索栏设置为
self.navigationItem.titleView = searchBar
也试过,
navigationController?.navigationBar.topItem?.titleView = searchBar
titleView 显示在 viewDidLoad() 和 viewDidAppear() 上,但是当进入后台并返回应用程序时,搜索栏会消失。
解决方案
Use UISearchController
for UISearchBar
. Like,
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
}
}
You can get more Information here.
I hope this will help you.
推荐阅读
- typescript - 缩小联合的条件类型
- python - 为单词中的字母分配数值
- c++ - 为什么 decltype(captured_var) 的行为不符合预期?
- python - 激活 bot.polling,同时激活命令处理程序。Telebot,蟒蛇 3
- python - 该程序将“”添加到我正在索引的字符串中
- javascript - 使用 Google 地球引擎时出现“生成图表时出错”
- r - 如何在我自己的包中使用 data.table::setDTthreads()?
- algorithm - K 个桶中 N 个对象的所有可能组合
- excel - 为什么我的 excel 宏不调用外部程序
- c++ - 模拟鼠标悬停在任何 Windows 应用程序的任何 UI 元素上