Don't display the UISearchController table until the search button is tapped - ios

It seems that the default behavior of the UISearchController is to display an empty table as soon as characters are entered in the UISearchBar. The search I'm implementing doesn't search as the characters are entered, but it searches when the Search button is tapped.
Because of this, I don't want to display an empty table until the actual search happens. How do I make this happen?
I've tried to set the table hidden until the search button is tapped, but it doesn't seem like the empty table that appears is the same as the UISearchController table, because none of the datasource methods are called on that table.

The empty table view you are seeing is from your search results controller, which is "immediately" displayed by UISearchController "when the user enters text in the search bar". (source)
Hiding the table view in your search results controller until you are ready to show it would seem to be the easiest solution. But as you discovered, upon displaying the search results controller, UISearchController sets the value of the isHidden property on the root view to false. In my debugging session, UISearchController behaved this way every time the search bar began editing or the search text changed, by calling a private method named _updateVisibilityOfSearchResultsForSearchBar:.
You can work around that behaviour by adding the table view in the search results controller as a subview of the root view (and then hiding or showing the table view as necessary), or by adjusting the value of the alpha property for the root view (which, unlike isHidden, can be animated).

In this case, you can do work around like creating a UIView and adding as subview over the tableview to make it hidden. Once user clicks on search button remove that view.

Related

Benefits of using UISearchController?

What's the benefit of using UISearchController over UISearchBarDelegate? The difference seems kind of subtle to me. In particular, updateSearchResults seems essentially the same as textDidChange. Assuming we don't use a new VC to display the search results, I can't see why one should add UISearchController on top of UISearchBarDelegate.
From the documentation for UISearchController:
You use a search controller in tandem with your existing view controllers. When you have a view controller with searchable content, incorporate the search bar of a UISearchController object into your view controller’s interface. When the user interacts with that search bar, the search controller automatically displays a new view controller with the search results that you specify.
Each search controller provides a UISearchBar object that you must incorporate into the user interface of your initial view controller. Add this object to the view containing your searchable contents. When the user taps the search bar to enter a search term, the search controller automatically displays your search results view controller and notifies your app that the search process has begun.
Basically, the search controller handles the grunt work of displaying the search results controller, animating the search bar to the header, graying out the results if there is nothing entered, etc.. It does a lot of the grunt work for you when switching between your regular view controller and the results controller.
When you don't need (or want) all of that helpful work done for you and you want to show your own search bar, or your main view controller can easily show the search results, then you don't need to use a UISearchController.
This is similar to asking why you should use a UINavigationController when you can add your own UINavigationBar and delegate or why use a UITabBarController when you can add your own UITabBar and delegate. It's all a matter of convenience and user interface consistency.

Do you have to put the UISearchBar in a UITableView?

I would like to use a UISearchBar and put it towards the bottom of a screen in order to search for available names through an API call. I want to have this SearchBar as part of a sign up form. However, when I put the SearchBar on the screen where I would like through the Storyboard, it does not show up when I run the app on the simulator. When I looked up this issue, everyone is putting the searchbar in a tableview. Am I not using the correct UI element for my cause?
The reason your search bar is not on the screen is probably because you didn't set constraint correctly or it was hidden or covered by some other view.
And for your second half of the question, I myself never put a search bar on a UITableView itself. Some apps put a search bar on the first cell of a table view but you have to scroll to make it show up. I myself always prefer to put it on the navigation controller on the top of the screen so that it will always be there and ready for user to search anything.

Swift iOS search bar loads custom view

I am designing an iOS app with swift which looks like in the picture.
When the user taps on the search bar (first image), a new view should appear (second image) while the search bar stays focused. When the user puts in some search query the expanded view shall be updated. When the user selects a result, the expanded view should go away and show the table view with updated results (third image).
How can I model this custom search bar behavior?
I think the best way to achieve this scheme is to create 2 view..let's say for example A and B.
In view A there is a searchBar and your tableview with different cell. When searchBar got focused or is clicked view B is presented. When a cell is selected, view A is presented back calling maybe API for new data

return to selected row

I have a nav controller --> table view controller --> view controller. The selected row returns a detail view. What I would like is when the back button is clicked the table scrolls back to the selected row rather than back to the top.
I have been through both SO results and google but after many attempts it fails, either achieving nothing or being declared as unassignable; I've tried assigning index path to property and content offset. I have all the common tableview methods. And I believe I require viewdidappear.
How do i achieve this?

Search Display Controller tableview

I have a Search Bar and Search Display controller that I dragged from the Objects list on the right of Interface Builder. I placed this object on my DetailViewController's view. I set the delegate to be the DetailViewController. I have code in the DetailViewController to supply an NSArray of searched results to the tableview that shows up when text is typed in the search bar. Basically, the search bar is on top of the DetailView of a Master/Detail view....
Everything is working fine except how do I get access to the tableview so that I can graphically design the tableview and cells in the Interface Builder storyboard? I would like as much as possible to avoid designing views programmatically.
The search display controller's table view doesn't exist until your app runs and the search takes place! So there's nothing to design until then. You can't set the search display controller's searchResultsTableView; you can only read it, so obviously you'll have to do that in code.

Resources