How to programmatically expand/bring up a search bar? - ios

I have a view controller with a UISearchController that, when tapped, will expand and bring up the keyboard. I want the user to press a button (separate from the search controller), when then brings up the search bar without actually tapping it. Is this possible?

What you want to achieve is to make the searchBar become the first responder. You can do that with:
searchController.searchBar.becomeFirstResponder()

Related

Stop propagation cancel button UISearchBarController

I have a navigation bar with added searchbar icon.
Tapping the searchbar button instantiates the UISearchBarController.
Everything works fine, except that the 'Cancel' button tap propagates to the navigation bar button below (which closes the current modal).
Tapping cancel here:
Also taps done here:
I've witnessed that there is a delegate method available:
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
}
How do I stop the event from propagating?
Note: The propagation doesn't happen when tapping 'Cancel' after text was entered in the searchbar, only when it is empty. Very strange.
I think what you are seeing is that tapping Cancel when text is present is actually clearing out the text within the UITextField that the UISearchBar owns (as part of the UISearchController). Tapping Cancel otherwise is supposed to dismiss the search bar. From the Apple docs on searchBarCancelButtonClicked: it states that:
Typically, you implement this method to dismiss the search bar.
I've ran into this in the past with "search bar" functionality on a UITableViewController, and it's one of the reasons why I've switched over to using a standard UIViewController which owns a UITableView and owns a UISearchBar. I have a little bit more control, this way, over the content of my view controller, and the search bar I'm using with it.
Not sure if that directly helps you, but using a UISearchBar instead of a UISearchController might point you in a better direction like it did for me.
Rebooting the emulator fixed the problem, whatever that makes sense.

Removing focus from Search Controller when view is loaded

I have a simple TableView with a search bar added. Everything works fine, except that when the view comes up (it's the first view in the app), the search bar is focused (or appears so), so that it shows the "Cancel" button (see image).
I've tried setting the tableview as first responder, calling resignFirstResponder on the search controller etc.
If you tap on the search bar and then hit cancel, the focus is removed and everything's fine - the "Cancel" button goes away.
Any ideas on why this is and how can I show a search bar that's not focused?
Add searchBar.resignFirstResponder() in viewDidLoad() method.
-Good Luck.
Based on the image, your textfield is not first responder and you just don't want to see cancel button
you can set searchBar.setShowsCancelButton = false
or in storyboard, uncheck Shows Cancel Button
then if you want to show cancel button during editing, check the UISearchBarDelegate methods:
func searchBarTextDidBeginEditing
and set searchBar.setShowsCancelButton = true
func searchBarCancelButtonClicked
and set searchBar.setShowsCancelButton = false

Search display controller disappears on multiple clicks on cancel button

I have a UITableView in which i have added UISearchDisplayController on the header of the table view in xib.
When i run the project and click on search bar textfield which show cancel button. When i click on cancel button multiple times frequently then UISearchBar is disappear from the header of the table view.
You can download the sample app from below url.
https://www.dropbox.com/s/3ero6u76dceq4r7/SearchBarSample.zip?dl=0
Any help is much appreciated. Thanks!
You have taken the search bar inside tableview. Just take it out of the tableview. Search bar should be above in list. The problem will be resolved.enter image description here

Swift - create a back button without navigation controller

I have an app that has a toolbar, but I don't want the bar at the top, in order to free more viewing space. Therefore I have decided not to use a navigation controller. I'd like to add a back button to the toolbar. How would I go about this?
Adding the button is easy enough, and setting the action to performSegueWithIdentifier is all fine, but what happens is that the previous view just gets loaded again, rather than show it as it was, like a true back button. So if I tap on the 10th row on a tableView and go to a new page, when I press the back button it loads the view from the top again, instead of showing it as where I scrolled down to last.
Even though you don't want a UINavigationBar, you do want a UINavigationController in this case, because it manages the 'back stack' exactly the way you want it. Just hide its navigation bar by setting its navigationBarHidden property to true (in the Storyboard or in the viewDidLoad function of the root view controller).
You can then use navigationController.popViewController(true) as normal, in response to the user clicking your custom back button.

ios UISearchBar hide overlay & prevent hiding navigation & tabbar

two little questions regarding the UISearchBar/SearchDisplayController:
Is it possible to prevent/hide the overlay which appears when a searchbar is clicked?
If the Searchbar is entered, the Tabbar and the NavigationBar of the current Screen disappears (and dont reappear after leaving the SearchBar) - how can i prevent that?
Is it possible to prevent/hide the overlay which appears when a
searchbar is clicked?
Why don't you just use a UISearchBar with the current table and not a UISearchDisplayController? One of the main features of the UISearchDisplayController is it's modal type display. With only the UISearchBar, you can search the current view.

Resources