Add filter button to UISearchController

流过昼夜 提交于 2019-12-03 12:29:45

问题


I want to add a "filter" or "sort" button next to the searchBar inside a UISearchController. I have already tried to add the UIButton and the searchbar into a UIView and set this as my UITableView tableView.tableHeaderView This does not work because the tableView.tableHeaderView get's set back to the UISearchBar only when dismissing the controller. Here is an example of how I want it to look:


回答1:


I could create a search filter with a few lines of code. I hope it helps.

*I chose the bookmark button to apply filter icon; because I have already used cancel button for custom action.

First, you have to delegate searchBar to handle bookmark button click action. Then you should set properties as below. If you want to give custom position (default position is right side), you need to setPositionAdjustment.

searchController.searchBar.delegate = self
searchController.searchBar.showsBookmarkButton = true
searchController.searchBar.setImage(UIImage(named: "Sort"), for: .bookmark, state: .normal)
// MARK: You may change position of bookmark button.
//searchController.searchBar.setPositionAdjustment(UIOffset(horizontal: 0, vertical: 0), for: .bookmark)

Then, override searchBarBookmarkButtonClicked function inside your VC. You can handle click event inside this method.

func searchBarBookmarkButtonClicked(_ searchBar: UISearchBar) { //showAlert, presentVC or whatever you want here }

Also if you want to hide bookmark button while search controller is active, you need to apply control inside updateSearchResults method.

if searchController.isActive {
    searchController.searchBar.showsBookmarkButton = false
} else {
    searchController.searchBar.showsBookmarkButton = true
}


When searchController is not active:


When searchController is active:




回答2:


Instead of customising the UISearchBar and complicating things, you can keep it simple by adding your own button beside SearchBar.

Set No the Shows cancel button option for SearchBar using

self.searchBar.showsCancelButton = true

OR

uncheck Shows cancel button option for SearchBar in StoryBoard

Add your own button beside SearchBar as shown below

And then accordingly add IBaction to the button and perform your actions in it.

Also in ideal case, it is not recommendable to put this view in Header of tableview. It will be better if it is above the tableview as also followed by Apple.




回答3:


I tried lot of way's to customize searchbar but nothing helped me at last found a way to full-fill your requirement

let mysearchController = UISearchController(searchResultsController: nil)
    mysearchController.searchBar.showsCancelButton = true
    mysearchController.searchBar.barStyle = UIBarStyle.Black
    for var subView1 in mysearchController.searchBar.subviews{
        for var subView2 in subView1.subviews{
            if subView2 .isKindOfClass(UIButton){
                let customizedCancelButton:UIButton = subView2 as! UIButton
                customizedCancelButton.enabled = true
                customizedCancelButton.setTitle("", forState: UIControlState.Normal)
                 let image1 = UIImage(named: "Sort")
                customizedCancelButton.setBackgroundImage(image1, forState: UIControlState.Normal)
                customizedCancelButton.addTarget(self, action: "sort", forControlEvents: UIControlEvents.TouchUpInside)
            }
        }
    }

above code is much simple to understand.

    func sort(){
       print("sort button fired!!!")
    }




回答4:


I'd lose the search controller, it doesn't offer any features you want really and the additional view / VC that you need to supply adds complexity / code duplication.

Your existing table view already have everything you need, because it has a data source. That's either an array or an FRC. If it's an array then you can replace the contents of the array with your filtered contents, and if it's an FRC you can change the predicate.

Note: in the array case, have 2 array references, 1 that points to an array holding all your unfiltered items and another that is initially a reference to the same array - the second reference is used to populate the table view. Then, when you filter, you create a new array with only the filtered items and point the second reference at that.

Instead of implementing the search controller delegate method you implement the search bar delegate methods and use that to trigger filtering and reset.

Now you've gotten rid of the search controller you also aren't switching table views, so you only have 1 TV and 1 header view. Now there is no reason to change the header view and everything just works.



来源:https://stackoverflow.com/questions/35037578/add-filter-button-to-uisearchcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!