searchBarSearchButtonClicked not working IOS - ios

I am trying to use the search bar to print out a string but the delegate function i have written does not work and I really do not understand where I am going wrong. Please can someone advise?
class LandingPage: UIViewController, UISearchBarDelegate{
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
print("search bar button clicked! ")
}

If you have added the search bar in storyboard then you have to ctrl+drag to ViewController the yellow button and select search bar delegate. if you have created the search bar in code then you have to assign it's delegate in your viewcontroller.swift
searchbar.delegate = self

Related

UISearchBar ends editing, hides keyboard, but screen is still dimmed

I have a view controller in which I dynamically create a UISearchController and assign it to self.navigationItem.searchController.
class MyViewController: UIViewController, UISearchBarDelegate {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.searchController = searchController
searchController.searchBar.delegate = self
// launch the app directly into this search text box
searchController.searchBar.becomeFirstResponder()
}
func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool {
// I tried this
searchBar.resignFirstResponder()
// and this
searchBar.endEditing(true)
// and finally this
return true
}
}
The keyboard hides away when the user touches anywhere outside the searchbar and the screen goes back to its brightness. That is the expected behavior.
However, when the user taps the [Search] button within the on-screen-keyboard, the keyboard goes away, but the screen is kept dim. None of the sub-views are useable, except if the user taps the search bar again, then the keyboard comes back.
So in short, the only way for the user to continue using the view controller, is not to hit the [search] button, which is counter-intuitive.
am I missing something?
In viewDidLoad(), add the following line:
searchController.obscuresBackgroundDuringPresentation = false
From the documentation: If you use the same view controller to display the searchable content and search results, it is recommended that you set this property to false. The default value of this property is true.

Search bar cancel button delegate not called

I implemented a search bar inside a navigation controller, it's working fine but the cancel button tap delegate method is not being called. Please help:
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
//This function is not being called
}
let searchBarCnt = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchBarCnt
navigationItem.hidesSearchBarWhenScrolling = true
self.navigationController?.view.backgroundColor = UIColor.white
searchBarCnt.delegate = self
searchBarCnt.searchBar.delegate = self
The function delegate is not being called because you are missing the definesPresentationContext:
Determines which parent view controller's view should be presented over for presentations of type
UIModalPresentationCurrentContext. If no ancestor view controller has this flag set, then the presenter
will be the root view controller.
you may enable such flag in this way:
import UIKit
class ViewController: UIViewController, UISearchBarDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.searchController = UISearchController(searchResultsController: nil)
self.navigationItem.searchController?.searchBar.delegate = self
self.definesPresentationContext = true
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
print("searchBarCancelButtonClicked")
}
}
Note:
without definesPresentationContext, you are not really touching the cancel button (when you try to touch it), you are just dismissing the context (you may notice a "silent" glitch in the background focus), suchlike
a popover is being dismissed.
When you click the Cancel button which appears next to search bar delegate method won't be called.
Try to click on search button on keyboard and check whether cancel button delegate method is getting called or not

UISearchBarDelegate function -searchBarSearchButtonClicked not working

I am trying to use a delegate function searchBarSearchButtonClicked from UISearchBarDelegate. My search bar is located in the header of UITableViewController. Please can someone advise?
class MySearchTableViewController: UITableViewController, UISearchBarDelegate{
override func viewDidLoad() {
super.viewDidLoad()
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
print("clicked!")
}
}
You need to set the delegate of search bar.
(1) mySearchBar.delegate = self
(2) In storyboard, ctrl+drag the search bar to the viewController and choosing delegate.
Please take outlet for your searchbar and confirm delegate.
yoursearchbar.delegate = self
Thanks

how to show the searchResultsController of UISearchController when the searchBar is pressed

enter image description hereI have to type some words to make the searchResultsController show,but what I want to do is showing the searchResultsController as soon as the searchBar is active.How can I do it ?
The only way I know how to do this is by implementing the UISearchBarDelegate and then implement searchBarShouldBeginEditing.
What you can do then is something like this:
func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
self.tabBarController?.selectedIndex = 1
return false
}
Unless the search view controller isn't situated in the tabbar, then you can just tell the navigationController to slide the searchResultsController into view. In the searchResultsController you can tell the searchbar to start editing with:
searchbar.becomeFirstResponder()
Just make sure to be aware from where the user comes from because I'd imagine it might be annoying if it would get called everytime viewDidAppear gets triggered.

How to hide the transition searchBar next screen

Added The Search bar TableView, but when one of the items that were looking for and the transition to his page SearchBar and module typing is not removed and remains on the screen. How can we make the transition to searchbar module typing cleaned?
If Have you have used UISearchController,put viewWillDisappear: in viewController.
//this is my searchController, created programatically
var searchController : UISearchController?;
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated);
if let sc = self.searchController where sc.active {
self.searchController?.dismissViewControllerAnimated(false, completion: nil);
}
}
Reason :
UISearchController is subclass of UIViewController.
If your search bar is active, that means it over your current viewController. so before going out from your viewController you need to check if it is active or not, by using its property .active.

Resources