iOS Swift: SearchController with a StoryBoard UISearchBar - ios

Evening, I have built a search controller, and I have also the code to programmatically create his search bar. But I would like to replace this code with a search bar designed in the story board.
So my question is, how can I connect the outlet to the search controller?
this is my code:
public class CustomSearchController: UISearchController {
public var customSearchBar = UISearchBar()
override public var searchBar: UISearchBar {
get {
return self.customSearchBar
}
}
}
func configureSearchController() {
searchController = CustomSearchController()
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.customSearchBar = self.customSearchBar
searchController.searchBar.delegate = self
self.definesPresentationContext = true
}
extension EarthSearcherTableViewController : UISearchResultsUpdating {
public func updateSearchResults(for searchController: UISearchController) {
//Code
guard let text = searchController.searchBar.text else { return }
self.getLocations(forSearchString: text)
}
fileprivate func getLocations(forSearchString searchString: String) {
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchString
request.region = mapView.region
let search = MKLocalSearch(request: request)
search.start { (response, error) in
guard let response = response else { return }
self.locations = response.mapItems
self.tableView.reloadData()
}
}
#objc func zoomToCurrentLocation() {
//Clear existing pins
mapView.removeAnnotations(mapView.annotations)
mapView.removeOverlays(mapView.overlays)
let annotation = MKPointAnnotation()
annotation.coordinate = mapView.userLocation.coordinate
mapView.addAnnotation(annotation)
let span = MKCoordinateSpanMake(0.005, 0.005)
let region = MKCoordinateRegionMake(mapView.userLocation.coordinate, span)
mapView.setRegion(region, animated: true)
let location = CLLocation(latitude: mapView.userLocation.coordinate.latitude, longitude: mapView.userLocation.coordinate.longitude)
mapView.add(MKCircle(center: location.coordinate, radius: 50))
}
}
I think I have a problem with the delegates, because when I type in the search bar, the results does not show off in the table
Any tips?

Subclass UISearchController and override searchBar getter to return the searchBar that you want.
public class mySearchController: UISearchController {
public var customSearchBar = UISearchBar()
override public var searchBar: UISearchBar {
get {
return customSearchBar
}
}
}
In your method, set the customSearchBar to your searchBar.
func configureSearchController() {
searchController = mySearchController()
searchController.customSearchBar = self.searchBar
//Other stuff...
}

Related

MapKit Map with child view won't add annotations

I'm currently trying to implement a Map connected with a search function. For the overlay containing the table view, I've decided to go for a library called FloatingPanel. Anyways, this shouldn't be of importance, since it shouldn't affect the essential code etc.
The data is being read inside of SearchResultsTableViewController and passed to MapViewController by passData().
Inside of passData() a function called addAnnotationToMap() from MapViewController is being called to process the data - printing inside of the function will always return the correct value.
Inside of the function I'm trying to add an annotation to the map, but it won't work. Nothing happens - but when I do print(mapView.annotations) the Array of annotations is being returned including mine.
By the way, I had to add loadViewIfNeeded() to the MapViewController because without (after using the overlay view) mapView returned nil.
Sorry for the amount of code - but there might be some relevant code. I didn't include the code for the table view.
MapViewController
class MapViewController: UIViewController, FloatingPanelControllerDelegate, UISearchBarDelegate {
var fpc: FloatingPanelController!
var searchVC = SearchResultTableViewController()
private enum AnnotationReuseID: String {
case pin
}
#IBOutlet private var mapView: MKMapView!
var mapItems: [MKMapItem]?
var boundingRegion: MKCoordinateRegion?
override func viewDidLoad() {
super.viewDidLoad()
if let region = boundingRegion {
mapView.region = region
}
fpc = FloatingPanelController()
fpc.delegate = self
// Initialize FloatingPanelController and add the view
fpc.surfaceView.backgroundColor = .clear
fpc.surfaceView.cornerRadius = 9.0
fpc.surfaceView.shadowHidden = false
searchVC = (storyboard?.instantiateViewController(withIdentifier: "SearchPanel") as! SearchResultTableViewController)
// Set a content view controller
fpc.set(contentViewController: searchVC)
fpc.track(scrollView: searchVC.tableView)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Add FloatingPanel to a view with animation.
fpc.addPanel(toParent: self, animated: true)
fpc.move(to: .tip, animated: true)
// Must be here
searchVC.searchController.searchBar.delegate = self
}
func addAnnotationToMap() {
loadViewIfNeeded()
guard let item = mapItems?.first else { return }
guard let coordinate = item.placemark.location?.coordinate else { return }
let annotation = MKPointAnnotation()
annotation.title = item.name
annotation.coordinate = coordinate
mapView.addAnnotation(annotation)
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
searchBar.showsCancelButton = false
fpc.move(to: .tip, animated: true)
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchBar.showsCancelButton = true
searchVC.tableView.alpha = 1.0
fpc.move(to: .full, animated: true)
searchVC.hideHeader()
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
fpc.move(to: .half, animated: true)
searchVC.showHeader()
}
func floatingPanelDidMove(_ vc: FloatingPanelController) {
let y = vc.surfaceView.frame.origin.y
let tipY = vc.originYOfSurface(for: .tip)
if y > tipY - 44.0 {
let progress = max(0.0, min((tipY - y) / 44.0, 1.0))
self.searchVC.tableView.alpha = progress
}
}
func floatingPanelWillBeginDragging(_ vc: FloatingPanelController) {
if vc.position == .full {
searchVC.searchBar.showsCancelButton = false
searchVC.searchBar.resignFirstResponder()
}
}
func floatingPanelDidEndDragging(_ vc: FloatingPanelController, withVelocity velocity: CGPoint, targetPosition: FloatingPanelPosition) {
UIView.animate(withDuration: 0.25,
delay: 0.0,
options: .allowUserInteraction,
animations: {
if targetPosition == .tip {
self.searchVC.tableView.alpha = 0.0
self.searchVC.hideHeader()
} else if targetPosition == .half {
self.searchVC.tableView.alpha = 1.0
self.searchVC.showHeader()
} else {
self.searchVC.tableView.alpha = 1.0
self.searchVC.hideHeader()
}
}, completion: nil)
}
}
SearchViewController
class SearchResultTableViewController: UIViewController {
#IBOutlet weak var searchBar: UISearchBar!
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var visualEffectView: UIVisualEffectView!
private enum CellReuseID: String {
case resultCell
}
private var places: [MKMapItem]? {
didSet {
tableView.reloadData()
}
}
private var suggestionController: SuggestionsTableTableViewController!
var searchController: UISearchController!
private var localSearch: MKLocalSearch? {
willSet {
places = nil
localSearch?.cancel()
}
}
private var boundingRegion: MKCoordinateRegion?
override func awakeFromNib() {
super.awakeFromNib()
suggestionController = SuggestionsTableTableViewController()
suggestionController.tableView.delegate = self
searchController = UISearchController(searchResultsController: suggestionController)
searchController.searchResultsUpdater = suggestionController
searchController.searchBar.isUserInteractionEnabled = false
searchController.searchBar.alpha = 0.5
}
override func viewDidLoad() {
super.viewDidLoad()
searchBar.addSubview(searchController.searchBar)
searchController.searchBar.searchBarStyle = .minimal
searchController.searchBar.tintColor = .black
searchController.searchBar.isUserInteractionEnabled = true
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
hideHeader()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if #available(iOS 10, *) {
visualEffectView.layer.cornerRadius = 9.0
visualEffectView.clipsToBounds = true
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
func showHeader() {
changeHeader(height: 116.0)
}
func hideHeader() {
changeHeader(height: 0.0)
}
func changeHeader(height: CGFloat) {
tableView.beginUpdates()
if let headerView = tableView.tableHeaderView {
UIView.animate(withDuration: 0.25) {
var frame = headerView.frame
frame.size.height = height
self.tableView.tableHeaderView?.frame = frame
}
}
tableView.endUpdates()
}
func passData() {
guard let mapViewController = storyboard?.instantiateViewController(withIdentifier: "map") as? MapViewController else { return }
guard let mapItem = places?.first else { return }
guard let coordinate = mapItem.placemark.location?.coordinate else { return }
let span = MKCoordinateSpan(latitudeDelta: coordinate.latitude, longitudeDelta: coordinate.longitude)
let region = MKCoordinateRegion(center: coordinate, span: span)
mapViewController.boundingRegion = region
mapViewController.mapItems = [mapItem]
mapViewController.addAnnotationToMap()
}
private func search(for suggestedCompletion: MKLocalSearchCompletion) {
let searchRequest = MKLocalSearch.Request(completion: suggestedCompletion)
search(using: searchRequest)
}
private func search(for queryString: String?) {
let searchRequest = MKLocalSearch.Request()
searchRequest.naturalLanguageQuery = queryString
search(using: searchRequest)
}
private func search(using searchRequest: MKLocalSearch.Request) {
if let region = boundingRegion {
searchRequest.region = region
}
UIApplication.shared.isNetworkActivityIndicatorVisible = true
localSearch = MKLocalSearch(request: searchRequest)
localSearch?.start { [weak self] (response, error) in
if error == nil {
self?.passData()
} else {
self?.displaySearchError(error)
return
}
self?.places = response?.mapItems
self?.boundingRegion = response?.boundingRegion
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
}
private func displaySearchError(_ error: Error?) {
if let error = error as NSError?, let errorString = error.userInfo[NSLocalizedDescriptionKey] as? String {
let alertController = UIAlertController(title: "Could not find any places.", message: errorString, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alertController, animated: true, completion: nil)
}
}
}

SearchBar not showing on navigation bar

I'm trying to show the search bar on the navigation bar.
The search bar should show me the search results and then show me the map navigation details.
I tried the same code to another project and seems to work.
This happens with the latest version of Xcode 10 and iOS 12.
here is the code:
// Protocol for dropping a pin at a specified place
protocol HandleMapSearch: class {
func dropPinZoomIn(_ placemark:MKPlacemark)
}
class MapViewController: UIViewController {
#IBOutlet weak var mapView: MKMapView!
#IBAction func navigateButton(_ sender: Any) {
}
let locationManager = CLLocationManager()
var currentLocation: CLLocation?
var routeCoordinates = [CLLocationCoordinate2D]()
var resultSearchController: UISearchController!
var selectedPin: MKPlacemark?
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
mapView.showsUserLocation = true
let coordinateSpan = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
guard let userCoordinate = locationManager.location?.coordinate else { return }
let userRegion: MKCoordinateRegion = MKCoordinateRegion(center: userCoordinate, span: coordinateSpan)
// Zoom to user location
mapView.setRegion(userRegion, animated: true)
// Hide back button
self.navigationItem.setHidesBackButton(true, animated: false)
let locationSearchTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearchTable") as! LocationSearchTable
resultSearchController = UISearchController(searchResultsController: locationSearchTable)
resultSearchController.searchResultsUpdater = locationSearchTable
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search for places"
navigationItem.titleView = resultSearchController?.searchBar
resultSearchController.hidesNavigationBarDuringPresentation = false
resultSearchController.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
locationSearchTable.mapView = mapView
locationSearchTable.handleMapSearchDelegate = self
}
I get no errors and not console log messages

How to disable cancel button of UISearchController.seachBar when i tap at searchBar?

i want to disable right cancel button when a tap at search Bar.
Because of using Google Place search i put i should use searchController?.searchBar
I try to disable cancel button at
func presentSearchController(_ searchController: UISearchController) {
searchController.searchBar.showsCancelButton = false
}
but when i tap a searchBar i see how cancel button appear and disappear, thats looks ugly
Please give me advice!
override func viewDidLoad() {
super.viewDidLoad()
resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self
searchController = UISearchController(searchResultsController: resultsViewController)
searchController?.searchResultsUpdater = resultsViewController
searchController?.searchBar.sizeToFit()
navigationItem.titleView = searchController?.searchBar
searchController?.searchBar.placeholder = searchBarPlaceholderText
searchController?.searchBar.tintColor = #colorLiteral(red: 0.1019607843, green: 0.5490196078, blue: 1, alpha: 1)
searchController?.searchBar.delegate = self
searchController?.delegate = self
searchController?.searchBar.searchBarStyle = .prominent
definesPresentationContext = true
mapView.delegate = self
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
checklocationAuthorizationStatus()
locationManager.requestWhenInUseAuthorization()
locationManager.requestLocation()
locationManager.startUpdatingLocation()
}
func presentSearchController(_ searchController: UISearchController) {
searchController.searchBar.showsCancelButton = false
}
You can create a custom class and subclass UISearchBar and UISearchViewController.
For example:-
class CustomizedSearchBar: UISearchBar {
override func layoutSubviews() {
super.layoutSubviews()
setShowsCancelButton(false, animated: false)
}
}
Now Create Object of the CustomizedSearchBar and use it within other viewController.
Or you can create a customized searchViewController as follows:
class CustomizedSearchController: UISearchController, UISearchBarDelegate {
lazy var _searchBar: CustomSearchBar = {
[unowned self] in
let result = CustomSearchBar(frame: CGRectZero)
result.delegate = self
return result
}()
override var searchBar: UISearchBar {
get {
return _searchBar
}
}
}
Please follow this link for more detail information.

Search Controller move along tableview

code of search controller
it's working fine for searching
issue 1:
when I scroll tableView, I want to show search controller along with table view, search controller also gets scrolled with table view.
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
tableView.tableHeaderView = searchController.searchBar
searchController.delegate = self
searchController.searchBar.sizeToFit()
self.searchController.searchBar.delegate = self
tableView.tableHeaderView = nil
definesPresentationContext = true
}
update data of searching :
func updateSearchResults(for searchController: UISearchController) {
_ = kidsData
let searchToSearch = searchController.searchBar.text
if(searchToSearch == "")
{
self.kidsData = self.KidsDataDuplicate
}
else{
self.kidsData.removeAll()
let itemsarray = self.KidsDataDuplicate
var forkidsinArray = [String]()
for Kids in itemsarray {
forkidsinArray.append(Kids.name)
if(Kids.name.range(of: searchToSearch!, options: .caseInsensitive) != nil)
{
self.kidsData.append(Kids)
}
}
}
self.tableView.reloadData()
}
issue 2 :
Tableview navigation bar contains search button, when I click on search button at navigation bar, search controller should show its working at tableview header, but after scrolling tableview I want to show search controller at tableview starting cell.
I can try the scroll delegates it's not working for me
pls help me......!
this is code of search button at navigation
var launchBool: Bool = false {
didSet {
if launchBool == true {
Status = 1
tableView.tableHeaderView = searchController.searchBar
let indexPath = IndexPath(row: 0, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .middle, animated: true)
} else {
tableView.tableHeaderView = nil
myInt = 0
}
}
}
#IBAction func NAVSearchButton(_ sender: UIBarButtonItem) {
launchBool = !launchBool
}

ViewWillDisappear not getting called searchcontroller

When I'm in the middle of a search and then switch UItabs, ViewWillDisappear does not get called. Any idea as to why ViewWillDisappear does not get called when I have filtered results displaying and switch tabs?
func updateSearchResultsForSearchController(searchController: UISearchController) {
if self.searchController?.searchBar.text.lengthOfBytesUsingEncoding(NSUTF32StringEncoding) > 0 {
if let results = self.results {
results.removeAllObjects()
} else {
results = NSMutableArray(capacity: MyVariables.dictionary.keys.array.count)
}
let searchBarText = self.searchController!.searchBar.text
let predicate = NSPredicate(block: { (city: AnyObject!, b: [NSObject : AnyObject]!) -> Bool in
var range: NSRange = NSMakeRange(0, 0)
if city is NSString {
range = city.rangeOfString(searchBarText, options: NSStringCompareOptions.CaseInsensitiveSearch)
}
return range.location != NSNotFound
})
// Get results from predicate and add them to the appropriate array.
let filteredArray = (MyVariables.dictionary.keys.array as NSArray).filteredArrayUsingPredicate(predicate)
self.results?.addObjectsFromArray(filteredArray)
// Reload a table with results.
self.searchResultsController?.tableView.reloadData()
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(self.identifier) as! UITableViewCell
var text: String?
var imgtext:AnyObject?
if tableView == self.searchResultsController?.tableView {
if let results = self.results {
text = self.results!.objectAtIndex(indexPath.row) as? String
imgtext = MyVariables.dictionary[text!]
let decodedData = NSData(base64EncodedString: imgtext! as! String, options: NSDataBase64DecodingOptions(rawValue: 0) )
var decodedimage = UIImage(data: decodedData!)
cell.imageView?.image = decodedimage
}
} else {
text = MyVariables.dictionary.keys.array[indexPath.row] as String
}
cell.textLabel!.text = text
return cell
}
On the Load
override func viewDidLoad() {
super.viewDidLoad()
let resultsTableView = UITableView(frame: self.tableView.frame)
self.searchResultsController = UITableViewController()
self.searchResultsController?.tableView = resultsTableView
self.searchResultsController?.tableView.dataSource = self
self.searchResultsController?.tableView.delegate = self
// Register cell class for the identifier.
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: self.identifier)
self.searchResultsController?.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: self.identifier)
self.searchController = UISearchController(searchResultsController: self.searchResultsController!)
self.searchController?.searchResultsUpdater = self
self.searchController?.delegate = self
self.searchController?.searchBar.sizeToFit()
self.searchController?.hidesNavigationBarDuringPresentation = false;
self.tableView.tableHeaderView = self.searchController?.searchBar
self.definesPresentationContext = true
}
Had the same issue. viewWillDisappear is not called on the UITableViewController, but it is called in the UISearchController.
So I subclassed UISearchController and overrode the viewWillDisappear method. In my case I just needed to deactivate the search controller.
class SearchController: UISearchController {
override func viewWillDisappear(_ animated: Bool) {
// to avoid black screen when switching tabs while searching
isActive = false
}
}
Similar to #user5130344 I found that subclassing resolved my issue, although I found that isActive = false cleared the search bar where I wanted the search query to remain on returning to the view.
Here's my subclass instead - this fixed my issue with iOS 13 dismissing the parent view:
class MySearchController: UISearchController {
override func viewWillDisappear(_ animated: Bool) {
// to avoid black screen when switching tabs while searching
self.dismiss(animated: true)
}
}
I think its the problem with the xcode . Try to close it and reopen the project once again and try to run again

Resources