使UISearchBar像音乐应用程序一样解散 [英] Make UISearchBar dismiss like Music App
问题描述
注意 searchBar
放大镜和占位符文本如何在解雇时转移:
Notice how the searchBar
magnifying glass and placeholder text both shift over on dismissal:
有没有办法我可以在没有文字和图标移动?查看我想要实现的动画的音乐应用程序的动画。
Is there any way I could dismiss this search bar without the text and icon moving over? See the animation of the Music app for the animation I am trying to achieve.
我有一个 UISeachBar
在导航栏中单击搜索按钮时显示。
I have a UISeachBar
that gets presented when the search button is clicked in a nav bar.
@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的中心,因为视图在屏幕上向上移动。
Now when the cancel button is pressed, I want the whole view to move up without seeing the SearchBar icon and placeholder text slide to the center of the searchBar as the view moves up off screen.
我知道有方法 searchBarCancelButtonCl icked(searchBar :)
,但这似乎会自动解除SearchBar,我不知道如何控制它。
I know there is the method searchBarCancelButtonClicked(searchBar:)
, but this seems to automatically dismiss the SearchBar and I don't know how to control that.
如何使SearchBar像音乐应用中的那个一样消失?
How can I make the SearchBar dismiss like the one in the Music app?
编辑:这是我使用的唯一委托方法:
EDIT: Here is the only delegate method I am using:
func updateSearchResultsForSearchController(searchController: UISearchController) {
masterFilter.removeAll()
filterContentForSearchText(searchController.searchBar.text!)
tableView.reloadData()
}
推荐答案
这就是我实现这个目标的方法。这很简单。
This is how I have achieved this. It is simple and easy.
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版本。
This code is for old swift version.
我希望这会对您有所帮助。
I hope this will help you.
这篇关于使UISearchBar像音乐应用程序一样解散的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!