My UISearchBar moves to top (animated) --> want to disable - ios

I want implement an UISearchBar on my tableView.
My code (in viewDidLoad) :
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.searchBar.autocapitalizationType = .None
self.searchController.searchBar.autocorrectionType = .No
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = false
self.tableView.tableHeaderView = self.searchController.searchBar
When I click on the searchBar, this one move to top, like it wants hide the navigationBar:
I searched on many posts for an answer but nothing works. I want to disable this animation so that the searchBar doesn't move.

You should be able to prevent this by setting:
self.searchController.hidesNavigationBarDuringPresentation = false

You can think of the UISearchController as being presented modally when you start searching. This would work fine if you had a usual UINavigationController setup, however in a more "customized" UI like yours you may run into issues like the search controller attaching to a wrong view, etc.
I would suggest not to use UISearchController in your case and use a separate UISearchBar initialised with the rest of your interface (probably in a storyboard?), and then do the search manually. Implement the UISearchBarDelegate and add your own UITableView for the search results, if you can't simply filter your content in place.
Although looks like you have a tableView var — you could simply add another property, similar to the one you use as a UITableViewDataSource and store filtered data there. So whenever you have something in the UISearchBar you simply use a filtered data source when reloading table view data. For instance:
var data: [String]
var filteredData: [String]
And then use filteredData in the UITableViewDataSource:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredData.count
}
...
And then do something like this in the UISearchBarDelegate:
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
// Filter the data you have. For instance:
filteredData = data.filter({$0.rangeOfString(searchText).location != NSNotFound})
tableView.reloadData()
}

Paste this line in your viewDidLoad of presenting controller:
self.extendedLayoutIncludesOpaqueBars = true

Related

Changing content of UICollectionView with segmented Control in the header in Swift

I tried to solve this problem since hours and didn't find a proper solution. I want to have a custom UICollectionView with a segmented control in the header. Changing the segmented control index should render the cells differently. So far I can display the segmented control in the header of my collectionView but changing the content of the collectionView inside of my UserSearchHeader doesn't work.
I created a custom UICollectionView called UserSearchController which is a Subclass of UICollectionView and conforms the UICollectionViewDelegateFlowLayout protocol.
class UserSearchController: UICollectionViewController,
UICollectionViewDelegateFlowLayout, UISearchBarDelegate { ....
My custom collectionView UserSearchController has a custom header (UserSearchHeader) and custom cells (UserSearchCell).
collectionView?.register(UserSearchCell.self, forCellWithReuseIdentifier: cellId)
collectionView?.register(UserSearchHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerId")
The UserSearchHeader contains the segmented control.
class UserSearchHeader: UICollectionViewCell {
var segmentedControl: UISegmentedControl = {
let sc = UISegmentedControl(items: ["Username", "Hashtag"])
sc.translatesAutoresizingMaskIntoConstraints = false
sc.tintColor = UIColor.black
sc.selectedSegmentIndex = 0 // automatically highlight
// sc.addTarget(self, action: #selector(segmentedChange), for: .valueChanged)
return sc
}() .....
If the segmentedIndex is 0 I want to render the cells with the UserSearchCell class if the segmentedIndex is 1 I want to render it with a different cellClass (Clicking the segmented control). How can I achieve this behavior with these different classes. shall I use the cellForItem at method inside of the UserSearchController to check the status of the segmented control. But how can I do this when the segmented control is defined in the UserSearchHeader class .
override func collectionView(_ collectionView:
UICollectionView, cellForItemAt indexPath: IndexPath) ->
UICollect. ionViewCell { if segmentedControl.segmentedIndex = 0 ....}
tried using Use the scopeButtonTitles property?
mySearchController.searchBar.scopeButtonTitles = ["Username", "Hashtag"]
Have a look at this question for more details. Basically you use the
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
// respond to your scopeBar selection here
switch selectedScope {
case 0:
print("Username tab selected")
// do your stuff for Username here
case 1:
print("Hashtag tab selected")
// do your stuff for Hashtag here
default:
print("Someone added a tab!")
// you shouldn't have more than 2 tabs
}
EDIT:
Please find below the missing pieces described in the link provided initially. There is no need to add the header from a NIB.
First, you add properties for your search & results controllers:
typealias ResultVC = UserResultController //The class to show your results
var searchController: UISearchController!
var resultController: ResultVC?
Then, in viewDidLoad you add this:
// Search Results
resultController = ResultVC()
setupSearchControllerWith(resultController!)
if #available(iOS 9.0, *) {
searchController?.loadViewIfNeeded()
}
Then you need to add this function to your class:
func setupSearchControllerWith(_ results: ResultVC) {
// Register Cells
results.tableView.register(TableCell.self, forCellReuseIdentifier: "\(TableCell.self)")
results.tableView.register(UINib(nibName: "\(TableCell.self)", bundle: Bundle.main), forCellReuseIdentifier: "\(TableCell.self)")
// We want to be the delegate for our filtered table so didSelectRowAtIndexPath(_:) is called for both tables.
results.tableView.delegate = self
searchController = UISearchController(searchResultsController: results)
// Set Scope Bar Buttons
searchController.searchBar.scopeButtonTitles = ["Comics only", "Digital too"]
// Set Search Bar
searchController.searchResultsUpdater = self
searchController.searchBar.sizeToFit()
tableView.tableHeaderView = searchController.searchBar
// Set delegates
searchController.delegate = self
searchController.searchBar.delegate = self // so we can monitor text & scope changes
// Configure Interface
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = true
if #available(iOS 9.1, *) {
searchController.obscuresBackgroundDuringPresentation = false
}
// Search is now just presenting a view controller. As such, normal view controller
// presentation semantics apply. Namely that presentation will walk up the view controller
// hierarchy until it finds the root view controller or one that defines a presentation context.
definesPresentationContext = true
}
Finally, you add the function I was describing initially: searchBar:selectedScopeButtonIndexDidChange:

Add Search Bar to Existing Header Swift 3

I have been adding search boxes and search tab bars to my Swift 3 project based on the Ray Wenderlich tutorial (https://www.raywenderlich.com/113772/uisearchcontroller-tutorial)
This has worked well for most tables. However, one of my tables has a header that has been defined in the storyboard. If I use the code as shown below, it replaces the header that is defined in the storyboard, and also has some other side-effects.
I therefore want to know how to do one of the following:
a)append the search bar to the existing header using code
b)add a searchbar object in the storyboard and then set-up the outlet correctly so that my other search code references it.
I have the following code
In didload
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
searchController.searchBar.scopeButtonTitles = ["All", "Active", "Not Active"]
searchController.searchBar.delegate = self
tableView.tableHeaderView = searchController.searchBar
Extension above main class
extension ItemsViewController: UISearchResultsUpdating{
func updateSearchResults(for: UISearchController) {
filterContentForSearchText(searchText: searchController.searchBar.text!)
}
}
extension ItemsViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
//filterContentForSearchText(searchText: searchController.searchBar.text!)
filterContentForSearchText(searchText: searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])
}
}
Filter Code
func filterContentForSearchText(searchText: String, scope: String =
... filter logic ..
tableView.reloadData()
}
any thoughts would be much appreciated

How to display empty tableview by default when using UISearchController in Swift? [duplicate]

As I understand, the default behaviour of UISearchController is:
On tapping search bar, background is dimmed and 'cancel' button is shown. SearchResultsController is not shown till this point.
SearchResultsController is displayed only if search bar is not empty.
I want to display SearchResultsController even when search bar is empty but selected (i.e is case 1 above).
Simply put, instead of background dimming, I would like to show Search results.
Is there a way for doing this?
More Clarification:
I am not using UISearchController to filter results shown on the view on which it is shown, but some other unrelated results.
It will be like what facebook does on its 'News Feed'. Tapping on search bar shows search suggestions initially and then, when we start editing, it shows search results which might not be related to news feed.
You can simply implement the UISearchResultsUpdating protocol and set the results controller view to always show in updateSearchResultsForSearchController:
func updateSearchResultsForSearchController(searchController: UISearchController) {
// Always show the search result controller
searchController.searchResultsController?.view.hidden = false
// Update your search results data and reload data
..
}
This works because the method is called even when the search bar is activated, without any text.
If your searchBar is active but has no text, the underlying tableView results are shown. That's the built-in behavior, and the reason why searchResultsController is hidden for that state.
To change the behavior when search is active but not filtering, you're going to have to show the searchResultsController when it is normally still hidden.
There may be a good way to accomplish this via <UISearchResultsUpdating> and updateSearchResultsForSearchController:. If you can solve it via the protocol, that's the preferred way to go.
If that doesn't help, you're left with hacking the built-in behavior. I wouldn't recommend or rely on it, and it's going to be fragile, but here's an answer if you choose that option:
Make sure your tableViewController conforms to <UISearchControllerDelegate>, and add
self.searchController.delegate = self;
Implement willPresentSearchController:
- (void)willPresentSearchController:(UISearchController *)searchController
{
dispatch_async(dispatch_get_main_queue(), ^{
searchController.searchResultsController.view.hidden = NO;
});
}
This makes the searchResultsController visible after its UISearchController set it to hidden.
Implement didPresentSearchController:
- (void)didPresentSearchController:(UISearchController *)searchController
{
searchController.searchResultsController.view.hidden = NO;
}
For a better way to work around the built-in behavior, see malhal's answer.
Updated for iOS 13
From iOS13, we got system API support for this behaviour. You can set the property showsSearchResultsController = true
For iOS 12 and below
I am recently working on UISearchController. I want to show search history in searchResultsController when search bar is empty. So searchResultsController needs to show up whenever UISearchController gets presented.
Here, I use another solution to make the searchResultsController always visible by overriding the hidden property in a custom view.
for example, my searchResultsController is a UITableViewController. I create a VisibleTableView as a subclass of UITableView, and then change the UITableView custom class of searchResultsController to VisibleTableView in xib or storyboard. This way, my searchResultsController will never be hidden by UISearchController.
The good things here:
Easier to implement than KVO.
No delay to show searchResultsController. Flipping the hidden flag in "updateSearchResults" delegate method works, but there is a delay to show the searchResultsController.
It does't reset the hidden flag, so there is no UI gap/jumping between hidden and visible.
Swift 3 sample code:
class VisibleTableView: UITableView {
override var isHidden: Bool {
get {
return false
}
set {
// ignoring any settings
}
}
}
I have tried PetahChristian solution, the preload result did show up when we first focus the searchbar, but when we enter something then clear it, the preload results will not reappear.
I came up with another solution. We only need to add a delegate into SearchResultsController and call it when our searchController.searchBar.text is empty. Something like this:
SearchResultsController:
protocol SearchResultsViewControllerDelegate {
func reassureShowingList() -> Void
}
class FullSearchResultsViewController: UIViewController, UISearchResultsUpdating{
var delegate: SearchResultsViewControllerDelegate?
...
func updateSearchResultsForSearchController(searchController: UISearchController) {
let query = searchController.searchBar.text?.trim()
if query == nil || query!.isEmpty {
...
self.delegate?.reassureShowingList()
...
}
...
}
And in the controller contains the SearchController, we add our delegate:
self.searchResultsController.delegate = self
func reassureShowingList() {
searchController.searchResultsController!.view.hidden = false
}
With tricky things like this I recommend the sledge hammer approach! That is to detect when something tries to make it hidden and when it does, change it back. This can be done via KVO (Key Value Observing). This will work no matter what, without having to handle all the intricacies of the search bar. Sorry the code is complicated but KVO is an older style API but my code follows recommend practice. In your SearchResultsViewController put this:
static int kHidden;
#implementation SearchResultsViewController
-(void)viewDidLoad{
[super viewDidLoad];
[self.view addObserver:self
forKeyPath:#"hidden"
options:(NSKeyValueObservingOptionNew |
NSKeyValueObservingOptionOld)
context:&kHidden];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
// if it was our observation
if(context == &kHidden){
// if the view is hidden then make it visible.
if([[change objectForKey:NSKeyValueChangeNewKey] boolValue]){
self.view.hidden = NO;
}
}
else{
// if necessary, pass the method up the subclass hierarchy.
if([super respondsToSelector:#selector(observeValueForKeyPath:ofObject:change:context:)]){
[super observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context];
}
}
}
- (void)dealloc
{
[self.view removeObserver:self forKeyPath:#"hidden"];
}
// Here have the rest of your code for the search results table.
#end
This works in all cases including if the text is cleared.
Lastly, to prevent the table doing an ugly fade to grey then to white when the search activates, use this:
self.searchController.dimsBackgroundDuringPresentation = NO;
Swift 3 Version:
If your searchResultController is not nil and you are using a separate table view controller to show the search results, then you can make that table view controller conform to UISearchResultUpdating and in the updateSearchResults function, you can simply unhide the view.
func updateSearchResults(for searchController: UISearchController) {
view.hidden = false
}
Swift 4 Version:
func updateSearchResults(for searchController: UISearchController) {
view.isHidden = false
}
What is being hidden is the search results controller's view. Therefore it is sufficient to unhide it any time it might be hidden. Simply do as follows in the search results controller:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.view.isHidden = false
}
func updateSearchResults(for searchController: UISearchController) {
self.view.isHidden = false
// ... your other code goes here ...
}
Now the results view (i.e. the table view) is always visible, even when the search bar text is empty.
By the way, the iOS Mail app behaves like this, and I assume that's how it's implemented (unless Apple has access to some secret private UISearchController setting).
[Tested in iOS 10 and iOS 11; I didn't test on any earlier system.]
The Swift 2.3 version of #malhal's approach:
class SearchResultsViewController : UIViewController {
var context = 0
override func viewDidLoad() {
super.viewDidLoad()
// Add observer
view.addObserver(self, forKeyPath: "hidden", options: [ .New, .Old ], context: &context)
}
deinit {
view.removeObserver(self, forKeyPath: "hidden")
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if context == &self.context {
if change?[NSKeyValueChangeNewKey] as? Bool == true {
view.hidden = false
}
} else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
}
}
Swift 4 version of malhals answer:
class SearchController: UISearchController {
private var viewIsHiddenObserver: NSKeyValueObservation?
override func viewDidLoad() {
super.viewDidLoad()
viewIsHiddenObserver = self.searchResultsController?.view.observe(\.hidden, changeHandler: { [weak self] (view, _) in
guard let searchController = self else {return}
if view.isHidden && searchController.searchBar.isFirstResponder {
view.isHidden = false
}
})
}
}
Please note the [weak self]. Otherwise you would introduce a retain cycle.
I think you are mistaken.
SearchResultsController only appears when there are results. This is slightly different than your interpretation.
The results are loaded manually based on the text in the search bar. So you can intercept it if the search bar is empty and return your own set of results.
If you don't want to dim the results, set the dimsBackgroundDuringPresentation property to false.
This will make sure that the underlying content is not dimmed during a search.
You will also have to make sure you return results even when the searchText is empty otherwise an empty tableview will be displayed.
I spent a lot of time with this, and ultimately the solution I went with is like #malhals's, but the amount of code is significantly reduced by using facebook's KVOController: https://github.com/facebook/KVOController . Another advantage here is that if your searchResultsController is a UINavigationController then you don't need to subclass it just to add #malhal's code.
// always show searchResultsController, even if text is empty
[self.KVOController observe:self.searchController.searchResultsController.view keyPath:#"hidden" options:NSKeyValueObservingOptionNew block:^(id observer, UIView* view, NSDictionary *change) {
if ([change[NSKeyValueChangeNewKey] boolValue] == YES) {
view.hidden = NO;
}
}];
self.searchController.dimsBackgroundDuringPresentation = NO;
The easiest way is to use ReactiveCocoa with this extension https://github.com/ColinEberhardt/ReactiveTwitterSearch/blob/master/ReactiveTwitterSearch/Util/UIKitExtensions.swift
presentViewController(sc, animated: true, completion: {
sc.searchResultsController?.view.rac_hidden.modify({ value -> Bool in
return false
})
} )
where sc is your UISearchController
I really liked Simon Wang's answer and worked with it and this is what I did and it works perfectly:
I subclass the UISearchController in my custom class:
class CustomClass: UISearchController {
override var searchResultsController: UIViewController? {
get {
let viewController = super.searchResultsController
viewController?.view.isHidden = false
return viewController
}
set {
// nothing
}
}
}
Also make sure you don't have this anywhere in your code:
self.resultsSearchController.isActive = true
resultsSearchController is my UISearchController
Simply what I was using this case
func updateSearchResults(for searchController: UISearchController) {
if let inputText = searchController.searchBar.text, !inputText.isEmpty {
self.view.isHidden = false
}
}
where self.view is a view of "searchResultsController" during initialisation of UISearchController.
var searchController = UISearchController(searchResultsController: searchResultsController)

updateSearchResultsForSearchController not called in iOS 9

I am trying to write a simple UITableView with a UISearchBar. I have no problem when I drag and drop a UITableViewController. Everything works! (see code below)
override func viewDidLoad() {
super.viewDidLoad()
self.searchResultsController = UISearchController(searchResultsController: nil) //initialize the search controller
self.searchResultsController.searchResultsUpdater = self //the search controller updater is this view
self.searchResultsController.dimsBackgroundDuringPresentation = false
self.searchResultsController.searchBar.sizeToFit()
self.searchResultsController.searchBar.searchBarStyle = UISearchBarStyle.Minimal
self.searchResultsController.searchBar.showsCancelButton = true
self.tableView.tableHeaderView = searchResultsController.searchBar
self.tableView.reloadData()
}
But now, for experimental purpose, I used UIViewController, and added a UITableView, no problem with showing my records in the table.
Then, added a UISearchBar from the storyboard, set its delegate to the UIViewController, but the updateSearchResultsForSearchController method is not called when the user type in something.
It's like my UISearchController has no idea there is a UISearchBar, and what ever I type in, does not evoke the updating method. Do I have to tell the UISearchController that hey this is your UISearchBar?
So here's top of my code:
class SearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate
{
#IBOutlet weak var mySearchBar: UISearchBar!
#IBOutlet weak var myViewTable: UITableView!
let allElements = ["H", "Li", "Na", "K", "Rb", "Cs", "Fr"]
var filteredElemetns = [String]()
var searchResultsController = UISearchController() //create a new search controller
override func viewDidLoad()
{
super.viewDidLoad()
self.searchResultsController = UISearchController(searchResultsController: nil) //initialize the search controller
self.searchResultsController.searchResultsUpdater = self //the search controller updater is this view
self.searchResultsController.dimsBackgroundDuringPresentation = false
self.searchResultsController.searchBar.sizeToFit()
self.searchResultsController.searchBar.searchBarStyle = UISearchBarStyle.Minimal
self.searchResultsController.searchBar.delegate = self
//self.myViewTable.tableHeaderView = self.searchResultsController.searchBar
}
If I uncomment the last line, then I'll have two UISearchBar, which one is added by the storyboard and the other one with the last line code. The one I added, does not work, but the one at top of the myViewTable does.
Ok, I found the solution.
I used my filtering algorithm inside searchBar:textDidChange function. Then everything worked, and I don't need updateSearchResultsForSearchController function anymore. Here is my code:
func searchBar(searchBar: UISearchBar, textDidChange searchText: String)
{
print("filtering...")
self.filteredElemetns.removeAll(keepCapacity: false) //remove all the elements
let searchPredict = NSPredicate(format: "SELF CONTAINS [c] %#", self.mySearchBar.text!)
print(searchPredict)
let foundElements = (self.allElements as NSArray).filteredArrayUsingPredicate(searchPredict)
self.filteredElemetns = foundElements as! [String]
self.myViewTable.reloadData()
}
Of course, don't forget to make sure your class conforms to UISearchBarDelegate protocol.
I find this approach better than using a UITableViewController. One reason, if you search, you will find that many people have a problem with making UISearchBar the first responder. The only solution to that, I found, is calling becomeFirstResponder after a delay, which is really not a good programming approach.
But, with this approach, you can make an outlet of your UISearchBar and then easy make it the first responder in viewDidAppear.
I know some people might say no you can easily make the UISearchBar the first responder even if you use UISearchResultsController by doing something like:
self.searchResultsController.searchBar.becomeFirstResponder()
self.searchResultsController.active = true
But, believe me in iOS 8/9 it is not that simple, it won't work. Try it...

UISearchController: show results even when search bar is empty

As I understand, the default behaviour of UISearchController is:
On tapping search bar, background is dimmed and 'cancel' button is shown. SearchResultsController is not shown till this point.
SearchResultsController is displayed only if search bar is not empty.
I want to display SearchResultsController even when search bar is empty but selected (i.e is case 1 above).
Simply put, instead of background dimming, I would like to show Search results.
Is there a way for doing this?
More Clarification:
I am not using UISearchController to filter results shown on the view on which it is shown, but some other unrelated results.
It will be like what facebook does on its 'News Feed'. Tapping on search bar shows search suggestions initially and then, when we start editing, it shows search results which might not be related to news feed.
You can simply implement the UISearchResultsUpdating protocol and set the results controller view to always show in updateSearchResultsForSearchController:
func updateSearchResultsForSearchController(searchController: UISearchController) {
// Always show the search result controller
searchController.searchResultsController?.view.hidden = false
// Update your search results data and reload data
..
}
This works because the method is called even when the search bar is activated, without any text.
If your searchBar is active but has no text, the underlying tableView results are shown. That's the built-in behavior, and the reason why searchResultsController is hidden for that state.
To change the behavior when search is active but not filtering, you're going to have to show the searchResultsController when it is normally still hidden.
There may be a good way to accomplish this via <UISearchResultsUpdating> and updateSearchResultsForSearchController:. If you can solve it via the protocol, that's the preferred way to go.
If that doesn't help, you're left with hacking the built-in behavior. I wouldn't recommend or rely on it, and it's going to be fragile, but here's an answer if you choose that option:
Make sure your tableViewController conforms to <UISearchControllerDelegate>, and add
self.searchController.delegate = self;
Implement willPresentSearchController:
- (void)willPresentSearchController:(UISearchController *)searchController
{
dispatch_async(dispatch_get_main_queue(), ^{
searchController.searchResultsController.view.hidden = NO;
});
}
This makes the searchResultsController visible after its UISearchController set it to hidden.
Implement didPresentSearchController:
- (void)didPresentSearchController:(UISearchController *)searchController
{
searchController.searchResultsController.view.hidden = NO;
}
For a better way to work around the built-in behavior, see malhal's answer.
Updated for iOS 13
From iOS13, we got system API support for this behaviour. You can set the property showsSearchResultsController = true
For iOS 12 and below
I am recently working on UISearchController. I want to show search history in searchResultsController when search bar is empty. So searchResultsController needs to show up whenever UISearchController gets presented.
Here, I use another solution to make the searchResultsController always visible by overriding the hidden property in a custom view.
for example, my searchResultsController is a UITableViewController. I create a VisibleTableView as a subclass of UITableView, and then change the UITableView custom class of searchResultsController to VisibleTableView in xib or storyboard. This way, my searchResultsController will never be hidden by UISearchController.
The good things here:
Easier to implement than KVO.
No delay to show searchResultsController. Flipping the hidden flag in "updateSearchResults" delegate method works, but there is a delay to show the searchResultsController.
It does't reset the hidden flag, so there is no UI gap/jumping between hidden and visible.
Swift 3 sample code:
class VisibleTableView: UITableView {
override var isHidden: Bool {
get {
return false
}
set {
// ignoring any settings
}
}
}
I have tried PetahChristian solution, the preload result did show up when we first focus the searchbar, but when we enter something then clear it, the preload results will not reappear.
I came up with another solution. We only need to add a delegate into SearchResultsController and call it when our searchController.searchBar.text is empty. Something like this:
SearchResultsController:
protocol SearchResultsViewControllerDelegate {
func reassureShowingList() -> Void
}
class FullSearchResultsViewController: UIViewController, UISearchResultsUpdating{
var delegate: SearchResultsViewControllerDelegate?
...
func updateSearchResultsForSearchController(searchController: UISearchController) {
let query = searchController.searchBar.text?.trim()
if query == nil || query!.isEmpty {
...
self.delegate?.reassureShowingList()
...
}
...
}
And in the controller contains the SearchController, we add our delegate:
self.searchResultsController.delegate = self
func reassureShowingList() {
searchController.searchResultsController!.view.hidden = false
}
With tricky things like this I recommend the sledge hammer approach! That is to detect when something tries to make it hidden and when it does, change it back. This can be done via KVO (Key Value Observing). This will work no matter what, without having to handle all the intricacies of the search bar. Sorry the code is complicated but KVO is an older style API but my code follows recommend practice. In your SearchResultsViewController put this:
static int kHidden;
#implementation SearchResultsViewController
-(void)viewDidLoad{
[super viewDidLoad];
[self.view addObserver:self
forKeyPath:#"hidden"
options:(NSKeyValueObservingOptionNew |
NSKeyValueObservingOptionOld)
context:&kHidden];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
// if it was our observation
if(context == &kHidden){
// if the view is hidden then make it visible.
if([[change objectForKey:NSKeyValueChangeNewKey] boolValue]){
self.view.hidden = NO;
}
}
else{
// if necessary, pass the method up the subclass hierarchy.
if([super respondsToSelector:#selector(observeValueForKeyPath:ofObject:change:context:)]){
[super observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context];
}
}
}
- (void)dealloc
{
[self.view removeObserver:self forKeyPath:#"hidden"];
}
// Here have the rest of your code for the search results table.
#end
This works in all cases including if the text is cleared.
Lastly, to prevent the table doing an ugly fade to grey then to white when the search activates, use this:
self.searchController.dimsBackgroundDuringPresentation = NO;
Swift 3 Version:
If your searchResultController is not nil and you are using a separate table view controller to show the search results, then you can make that table view controller conform to UISearchResultUpdating and in the updateSearchResults function, you can simply unhide the view.
func updateSearchResults(for searchController: UISearchController) {
view.hidden = false
}
Swift 4 Version:
func updateSearchResults(for searchController: UISearchController) {
view.isHidden = false
}
What is being hidden is the search results controller's view. Therefore it is sufficient to unhide it any time it might be hidden. Simply do as follows in the search results controller:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.view.isHidden = false
}
func updateSearchResults(for searchController: UISearchController) {
self.view.isHidden = false
// ... your other code goes here ...
}
Now the results view (i.e. the table view) is always visible, even when the search bar text is empty.
By the way, the iOS Mail app behaves like this, and I assume that's how it's implemented (unless Apple has access to some secret private UISearchController setting).
[Tested in iOS 10 and iOS 11; I didn't test on any earlier system.]
The Swift 2.3 version of #malhal's approach:
class SearchResultsViewController : UIViewController {
var context = 0
override func viewDidLoad() {
super.viewDidLoad()
// Add observer
view.addObserver(self, forKeyPath: "hidden", options: [ .New, .Old ], context: &context)
}
deinit {
view.removeObserver(self, forKeyPath: "hidden")
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if context == &self.context {
if change?[NSKeyValueChangeNewKey] as? Bool == true {
view.hidden = false
}
} else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
}
}
Swift 4 version of malhals answer:
class SearchController: UISearchController {
private var viewIsHiddenObserver: NSKeyValueObservation?
override func viewDidLoad() {
super.viewDidLoad()
viewIsHiddenObserver = self.searchResultsController?.view.observe(\.hidden, changeHandler: { [weak self] (view, _) in
guard let searchController = self else {return}
if view.isHidden && searchController.searchBar.isFirstResponder {
view.isHidden = false
}
})
}
}
Please note the [weak self]. Otherwise you would introduce a retain cycle.
I think you are mistaken.
SearchResultsController only appears when there are results. This is slightly different than your interpretation.
The results are loaded manually based on the text in the search bar. So you can intercept it if the search bar is empty and return your own set of results.
If you don't want to dim the results, set the dimsBackgroundDuringPresentation property to false.
This will make sure that the underlying content is not dimmed during a search.
You will also have to make sure you return results even when the searchText is empty otherwise an empty tableview will be displayed.
I spent a lot of time with this, and ultimately the solution I went with is like #malhals's, but the amount of code is significantly reduced by using facebook's KVOController: https://github.com/facebook/KVOController . Another advantage here is that if your searchResultsController is a UINavigationController then you don't need to subclass it just to add #malhal's code.
// always show searchResultsController, even if text is empty
[self.KVOController observe:self.searchController.searchResultsController.view keyPath:#"hidden" options:NSKeyValueObservingOptionNew block:^(id observer, UIView* view, NSDictionary *change) {
if ([change[NSKeyValueChangeNewKey] boolValue] == YES) {
view.hidden = NO;
}
}];
self.searchController.dimsBackgroundDuringPresentation = NO;
The easiest way is to use ReactiveCocoa with this extension https://github.com/ColinEberhardt/ReactiveTwitterSearch/blob/master/ReactiveTwitterSearch/Util/UIKitExtensions.swift
presentViewController(sc, animated: true, completion: {
sc.searchResultsController?.view.rac_hidden.modify({ value -> Bool in
return false
})
} )
where sc is your UISearchController
I really liked Simon Wang's answer and worked with it and this is what I did and it works perfectly:
I subclass the UISearchController in my custom class:
class CustomClass: UISearchController {
override var searchResultsController: UIViewController? {
get {
let viewController = super.searchResultsController
viewController?.view.isHidden = false
return viewController
}
set {
// nothing
}
}
}
Also make sure you don't have this anywhere in your code:
self.resultsSearchController.isActive = true
resultsSearchController is my UISearchController
Simply what I was using this case
func updateSearchResults(for searchController: UISearchController) {
if let inputText = searchController.searchBar.text, !inputText.isEmpty {
self.view.isHidden = false
}
}
where self.view is a view of "searchResultsController" during initialisation of UISearchController.
var searchController = UISearchController(searchResultsController: searchResultsController)

Resources