My code for search bar delegate is:
extension ViewController: UISearchBarDelegate{
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
// change the array that is being printed
searching = true
print("here")
tableView.reloadData()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searching = false
searchBar.text = ""
print("cancel")
tableView.reloadData()
}
}
My search bar is connected as #IBOutlet weak var searchBar: UISearchBar!
Setting aside whether I'm filtering things right, right now neither typing into the search bar nor clicking cancel does anything (as shown above, at least when those methods get called "here" and "cancel" should be printed to the console), and cancel is not clearing the search bar text either. Does anyone know why this happens/what I did wrong?
Have you checked if you set the searchBar delegate as the ViewController? You've done right implementing the protocol, but make sure you have this code in your viewDidLoad function:
self.searchBar.delegate = self
Is also worth checking if the storyboard search bar element is really connected to the #IBOutlet declared on the ViewController. You can find this in the connections inspector. Yours must look like this:
Look at the second connection in this image
I have a searchbar, that opens a keyboard when selected.
The issue is, when I use VoiceOver, the keyboard stays open even when moving on to another element. For that, I wish to use the cancel button of the searchbar and make it appear only if accessibility is used, but when I make it appear, VoiceOver does not read it.
How can I make it accessible ? Or do you know any other way to handle properly the keyboard of a searchbar with VoiceOver ?
public func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
if UIAccessibilityIsVoiceOverRunning() {
self.searchBar?.setShowsCancelButton(true, animated: true)
}
}
public func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
self.searchBar?.showsCancelButton = false
}
I try to make my searchbar on swift, but I have a problem to dismiss keyboard on screen when I pressed out of searchbar. When I try with textfield, it works perfectly fine with this code.
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing(true)
}
It work when i press out of my textfield and then the keyboard is gone. I want to make like that with my searchbar, because when I use searchbar and use the same way like textfield, it doesn't work at all. Any reference or code is very useful for me.
try this :
self.mySearchController.searchBar.endEditing(true)
replace mySearchController with your created controller name..
If you did not create it programmatically but instead you just dragged a search bar from library then IBoutlet your searchable to your class and reference it as:
self.mySearchBar.endEditing(true)
I found it easier and simplier to use Table View for dismissal. (If you're using table view)
Swift 4:
self.tableView.keyboardDismissMode = .onDrag
Tested and working!
func searchBarSearchButtonClicked(searchBar: UISearchBar)
{
searchActive = false;
self.mySearchBar.endEditing(true)
}
Edit for Swift 4.2
func searchBarSearchButtonClicked(_ searchBar: UISearchBar)
{
searchActive = false
self.searchBar.endEditing(true)
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchActive = false;
searchProject.resignFirstResponder()
}
This method will be invoked when user click search button on keyboard.So here we can dismiss keyboard.I think this is the right method.
Firstly, Apple's UISearchBarDelegate is the correct solution to hide keyboard when users click a search button while UISearchBar's instance is the first responder (learn UIResponder). In short, searchBarSearchButtonClicked(_:) is what you need for this task.
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder() // hides the keyboard.
doThingsForSearching()
}
If it doesn't work, check, does your controller conform to UISearchBarDelegate and secondly, does UISearchBarDelegate know about your class implementation (if you don't quite understand what am I talking about, you should learn delegation pattern starting to read here):
class YourAwesomeViewController: UIViewController, UISearchBarDelegate { // pay attention here
#IBOutlet weak var yourSearchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
self.yourSearchBar.delegate = self // and it's important too
}
}
Further, if you need to hide the keyboard touching outside of search bar without touching the search button (the user may change his mind to search something), UITapGestureRecognizer is a simple way too to deal with that.
Ctrl-drag a Tap Gesture Recognizer from the Object Library to your View Controller.
Ctrl-drag the recently added Tap Gesture Recognizer from the document outline in the storyboard to your class implementation as IBAction.
Finally, write a code:
#IBAction func tapToHideKeyboard(_ sender: UITapGestureRecognizer) {
self.yourSearchBar.resignFirstResponder()
}
Also, don't forget to create #IBOutlet for the search bar to have an access inside your class implementation.
Both variants above work well in my project.
Swift 4+:
You can try, creating a tap gesture and add in the self.view
let singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.singleTap(sender:)))
singleTapGestureRecognizer.numberOfTapsRequired = 1
singleTapGestureRecognizer.isEnabled = true
singleTapGestureRecognizer.cancelsTouchesInView = false
self.view.addGestureRecognizer(singleTapGestureRecognizer)
and in selector func you call self.searchBar.resignFirstResponder
#objc func singleTap(sender: UITapGestureRecognizer) {
self.searchBar.resignFirstResponder()
}
You can use a general UIViewController extension
Just add a new swift file on the project and paste the following code snippet
Code
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard(_:)))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
#objc func dismissKeyboard(_ sender: UITapGestureRecognizer) {
view.endEditing(true)
if let nav = self.navigationController {
nav.view.endEditing(true)
}
}
}
Now call hideKeyboardWhenTappedAround() from viewDidLoad method where you want keyboard hiding feature.
class MaCaveViewController: UIViewController, UISearchBarDelegate {
#IBOutlet weak var searchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
}
// When button "Search" pressed
func searchBarSearchButtonClicked(_ searchBar: UISearchBar){
print("end searching --> Close Keyboard")
self.searchBar.endEditing(true)
}
}
This works very well for me.
we can do this with following methods
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchBar.showsCancelButton = true;
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchBar.showsCancelButton = false;
}
This works for me in Swift 4
func searchBarSearchButtonClicked(_ searchBar: UISearchBar){
self.searchBar.endEditing(true)
}
I have an action to call searchBar. When it is called, i want the keyboard to show up.
How can i show the keyboard?
This is the code with my action:
#IBAction func Searappear(sender: AnyObject) {
searchBar.hidden=false
navigationController?.navigationBarHidden = true
}
You should call searchBar.becomeFirstResponder().
I'm using UISearchBar when I input text on UISearchBar the keyboard shows. At that time, keyboard return key is "Search".
I want to implement event when I press the keyboard search button.
How can I implement the action?
On UITextField it has
-(BOOL)textFieldShouldReturn:(UITextField *)textField;
But on UISearchBar it doesn't have return action.
Thank you for your helping.
Add UISearchBarDelegate in .h
Also set SearchBar's object delegate to self.
Add this to the UISearchBarDelegate's method:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
// Do the search...
}
Swift
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
In Swift 3:
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
The textFieldShouldReturn method is a textField delegate method and not the one you're looking for. What you need is a UISearchBarDelegate method called searchButtonClicked, have a look here.
i Used following code in swift 5. implement UISearchBarDelegate then write following code in class keyboard will automatically hide on clicking search
class sample: UIViewController, UISearchBarDelegate {
let searchbar: UISearchBar = {
let sear = UISearchBar()
sear.tintColor = Mycolor().lightgray1
sear.backgroundColor = Mycolor().lightgray1
sear.barTintColor = Mycolor().lightgray1
sear.placeholder = "Search Asset"
sear.layer.cornerRadius = 30
sear.barStyle = .default
sear.backgroundImage = UIImage()
sear.returnKeyType = .search
// sear.addShadow(offset: CGSize(width: 10, height: 10), color: UIColor.darkGray, radius: 20, opacity: 5)
return sear
}()
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
}