ios - 不活动时从 UINavigationBar 中删除 UISearchController

标签 ios swift

当按下 UINavigationBar 按钮时,我试图添加一个 UISearchController。然后当再次按下按钮时,它会关闭搜索栏。到目前为止,我有:

@IBAction func showSearchBar(sender: UIBarButtonItem) {
    if searchController == nil {
        var resultsController = SearchResultsViewController()
        resultsController.searchResultsDelegate = self

        searchController = UISearchController(searchResultsController: resultsController)
        searchController!.searchResultsUpdater = resultsController

        searchController!.searchBar.frame = CGRect(
            x: searchController!.searchBar.frame.origin.x,
            y: searchController!.searchBar.frame.origin.y,
            width: searchController!.searchBar.frame.size.width,
            height: 44.0)
        searchController!.searchBar.center.x += self.view.bounds.width
    }

    if !searchBarActive {
        UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
            self.searchController!.searchBar.center.x -= self.view.bounds.width
        }, completion: nil)
        searchController?.hidesNavigationBarDuringPresentation = false
        searchController?.searchBar.searchBarStyle = UISearchBarStyle.Default
        searchController?.dimsBackgroundDuringPresentation = true
        navigationItem.titleView = searchController?.searchBar
        self.definesPresentationContext = true

    } else {
        UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
            self.searchController!.searchBar.center.x += self.view.bounds.width
        }, completion: nil)
        dismissViewControllerAnimated(true, completion: nil) // edited: tried this per comments but also does not work
        searchController!.active = false
    }

    searchBarActive = !searchBarActive
}

这确实添加了 searchController.searchBar 并将其从屏幕的 RHS 滑入。再次按下该按钮时,它会将搜索栏滑回右侧。但是,我的被搜索栏覆盖的后退按钮不会重置为“返回”字样,当我滑动 searchBar 关闭屏幕。我觉得我为了关闭 searchController 而遗漏了一些东西,但我不知道正确的调用是什么。searchController.active 属性似乎没有这样做。

最佳答案

调用 dismissViewControllerAnimated: 关闭 UISearchController 并在该方法的完成 block 中执行所有其他更改。

关于ios - 不活动时从 UINavigationBar 中删除 UISearchController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30159794/

相关文章:

ios - 如何调用api url方法,以便每次应用程序从后台进入前台时重新加载应用程序?

ios - 无法使基本的 socket.io 发出与 Swift 一起工作

iOS 8 键盘 - 显示键盘时向上推 UITextField 的问题

ios - fatal error : unexpectedly found nil - number of row

ios - 使用变量更改 UIButton 图像

ios - 如何在 iOS 中使用委托(delegate)

ios - 核心数据迁移崩溃

IOS __connection_block_invoke_2 Swift 2.0 xCode 7.1 El Capitan 10.1

swift - “弱”可能只适用于类和类绑定(bind)协议(protocol)类型,而不是 'ContentView' 我缺少什么?

ios - 为什么 UTTypeCreatePreferredIdentifierForTag 返回奇怪的 UTI?