ios - 当应用程序进入后台时,navigationItem 的 titleView 中的 searchBar 消失

标签 ios swift

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 在 vi​​ewDidLoad() 和 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/

相关文章:

ios - React Native map 标注按下在 IOS 上不触发

ios - Swift 3 和带抽屉导航的谷歌地图

iOS 解释 BLE 数据和字节 swift 3

ios - 如何检索firestore中的数据对象

ios - 如何比较不区分大小写的 NSRange ?

ios - MonoTouch项目导致Application Loader报错 "icon dimensions (0 x 0) don' t meet the size requirements”

ios - 在 ios 8 中将集合或表格 View 或自定义 View 添加到 ScrollView

swift - 快速获取字典元素编号

ios - 如何在 Swift 中创建当前状态?

ios - 为什么 viewDidLoad 在 vi​​ewWillAppear 之前被调用?