【问题标题】:Make UISearchBar dismiss like Music App让 UISearchBar 像 Music App 一样消失
【发布时间】:2015-08-29 15:54:03
【问题描述】:

注意searchBar 放大镜和占位符文本在解雇时是如何移动的:

有没有什么办法可以在不移动文本和图标的情况下关闭这个搜索栏?查看音乐应用的动画,了解我想要实现的动画。

我有一个UISeachBar,当在导航栏中单击搜索按钮时会显示它。

@IBAction func searchButtonPressed(sender: AnyObject) {

    searchController = UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater = self
    searchController.searchBar.delegate = self
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.dimsBackgroundDuringPresentation = false
    presentViewController(searchController, animated: true, completion: nil)
}

现在,当按下取消按钮时,我希望整个视图向上移动,而不会在视图向上移出屏幕时看到 SearchBar 图标和占位符文本滑动到 searchBar 的中心。

我知道有 searchBarCancelButtonClicked(searchBar:) 方法,但这似乎会自动关闭 SearchBar,我不知道如何控制它。

如何让 SearchBar 像音乐应用中的一样消失?

编辑:这是我使用的唯一委托方法:

func updateSearchResultsForSearchController(searchController: UISearchController) {
    masterFilter.removeAll()
    filterContentForSearchText(searchController.searchBar.text!)
    tableView.reloadData()
}

【问题讨论】:

  • 您可以发布您可能添加的任何UISearchBarDelegate 方法吗?如果你打电话给resignFirstResponder,它会触发不需要的动画。
  • 您已设置您的searchBar.delegate。但是您的代码中没有searchBarCancelButtonClicked 或其他searchBar 方法?在这一点上,我唯一能解释的是您的项目中的一些额外内容,这些内容是您尝试过的先前方法遗留下来的,这会影响您切换到的新方法。
  • 原谅我。我确实有searchBarCancelButtonClicked,但里面没有代码。我不确定要放什么代码来实现我想要的动画。
  • 无。您不应该包含它,甚至不需要设置searchBar.delegate,因为过滤是由 searchResultsUpdater 处理的。尝试删除 searchController.searchBar.delegate = self,因为 Apple 甚至没有在他们的示例中包含它。
  • 好的,我删除了那行。回到我原来的问题,这个动画应该怎么实现呢?

标签: ios swift uisearchcontroller


【解决方案1】:

这就是我实现这一目标的方式。简单易行。

extension UIViewController {


    func setNavigationBarItem() {

        let searchaButton = UIButton.init(frame: CGRectMake(0, 0, 30, 30))
        searchaButton.setBackgroundImage(UIImage(named: "search.png"), forState: UIControlState.Normal)
        searchaButton.addTarget(self, action: #selector(self.searchPressed), forControlEvents: UIControlEvents.TouchUpInside)


        let rightButton: UIBarButtonItem = UIBarButtonItem.init(customView: searchaButton)

        navigationItem.rightBarButtonItem = rightButton

    }


    public func searchPressed(){

        var searchController: UISearchController!

        // Create the search results view controller and use it for the UISearchController.
        let searchResultsController = storyboard!.instantiateViewControllerWithIdentifier("SearchResultController") as! SearchResultsViewController

        // Create the search controller and make it perform the results updating.
        searchController = UISearchController(searchResultsController: searchResultsController)
        searchController.searchResultsUpdater = searchResultsController
        searchController.hidesNavigationBarDuringPresentation = false

        // Present the view controller.
        presentViewController(searchController, animated: true, completion: nil)

    }

}

注意:

此代码适用于旧的 swift 版本。

希望对你有帮助。

【讨论】:

    【解决方案2】:

    searchBar 向右展开,因为正在取消搜索,并且取消按钮正在消失。

    无需取消搜索并触发该动画,只需显式关闭搜索控制器即可。

    你可以通过设置来做到这一点

    searchController.active = false
    

    【讨论】:

    • 把它放在searchBarCancelButtonClicked(searchBar:) 对动画没有影响。
    • 因为您在单击取消按钮后仍在取消搜索。同样,我建议您下载并运行 Apple 的 UICatalog sample code。它完全按照您的要求进行操作,并且以与 Music 完全相同的方式进行动画处理。看看他们如何解雇他们的搜索控制器。您会发现搜索并未被取消。我可以粘贴代码,但如果你自己发现它是如何工作的,你就会明白。
    • 我很想运行 UICatalog 项目,但是项目中有 4 个错误。它没有构建。
    • 您使用的是 Xcode 6 还是 7?它已更新到 Swift 1.2 并从 Xcode 6.3 开始工作。
    • 我使用的是 Xcode 6.4。它在 Xcode 7 中也不起作用。
    【解决方案3】:

    要使此动画正常工作,您需要了解自定义 UIViewController transitions。在UISearchBarDelegate 方法-searchBarCancelButtonClicked(searchBar:) 中,您需要做一些事情:

    1. HideUINavigationBarUISearchControllerdismissal
    2. UITableView 设置为动画到帧的底部并淡出。
    3. 如果您以模态方式显示搜索,请确保将 modalPresentationStyle 设置为 UIModalPresentationCurrentContext

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-22
      • 2012-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-26
      • 2014-01-20
      相关资源
      最近更新 更多