让UISearchBar像音乐应用程序一样被忽略

时间:2015-08-29 15:54:03

标签: ios swift uisearchcontroller

注意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()
}

3 个答案:

答案 0 :(得分: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版本。

enter image description here

我希望这会对你有所帮助。

答案 1 :(得分:0)

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

不要取消搜索并触发该动画,只需明确关闭搜索控制器。

您可以通过设置

来完成此操作
searchController.active = false

答案 2 :(得分:0)

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

  1. Hide UISearchController dismissal
  2. 上的UINavigationBar
  3. UITableView设置为框架底部的动画并淡出。
  4. 如果您以模态方式展示搜索,请确保将modalPresentationStyle设置为UIModalPresentationCurrentContext