Use UISearchController without storyboard - ios

I am relatively new to iOS (100 hours or so) and I am trying to implement a UISearchController without a storyboard. Is it possible to do this programmatically?

You have to implement UISearchController programmatically since Apple hasn't provided the ability to set it up using Storyboard yet.
Here are the steps to implement UISearchController in a view controller
Conform to UISearchResultsUpdating Protocol
Create a variable to reference the UISearchController
var searchController: UISearchController!
Set searchController in viewDidLoad(_:)
override func viewDidLoad() {
super.viewDidLoad()
searchController = UISearchController(searchResultsController: nil)
// The object responsible for updating the contents of the search results controller.
searchController.searchResultsUpdater = self
// Determines whether the underlying content is dimmed during a search.
// if we are presenting the display results in the same view, this should be false
searchController.dimsBackgroundDuringPresentation = false
// Make sure the that the search bar is visible within the navigation bar.
searchController.searchBar.sizeToFit()
// Include the search controller's search bar within the table's header view.
tableView.tableHeaderView = searchController.searchBar
definesPresentationContext = true
}
Implement the method updateSearchResultsForSearchController(_:), which is called when the search bar becomes the first responder or when the user makes changes to the text inside the search bar
func updateSearchResultsForSearchController(searchController: UISearchController) {
// No need to update anything if we're being dismissed.
if !searchController.active {
return
}
// you can access the text in the search bar as below
filterString = searchController.searchBar.text
// write some code to filter the data provided to your tableview
}

Related

UISearchBar in UINavigationController

I am wondering to programmatically put a UISearchBar into UINavigationController, rather than replacing UINavBar title, as I want the large title to show up. See Files app on iOS.
Here is my .swift
class Search: UITableViewController, UISearchBarDelegate
override func viewDidLoad() {
super.viewDidLoad()
searchBar()
}
func searchBar() {
let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController
navigationItem.searchController?.searchBar.placeholder = "search".localized
navigationItem.searchController?.dimsBackgroundDuringPresentation = false
navigationItem.hidesSearchBarWhenScrolling = false
}
The point is: by changing nil to actual searchResultsController, after typing text into search bar, the whole navigation controller (with search bar and typed text) got replaced by the searchResultsController view. No search bar while typing into it in short.
I am trying hard to use iOS 11/12 native look and feel within the app and putting the search bar into TableViewController is a no way here. Can someone help me?
If you don't need a different view controller to display the results simply use (it will not make your navigation bar disappear):
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
And do not change the value after that.
Most of your code is good, however you will need to implement the UISearchResultsUpdating protocol to detect updates (when the user write in the search bar) and use a table view (inside your view controller) to display the results of the search.
You do that by reloading the data of your table view inside the function: updateSearchResults(for searchController:) after filtering the data you want with the text inside the search bar. You can read this good tutorial on the subject for more information.

Unable to interact with view in iOS 11 SearchController?

I am unable to touch the view or scroll the tableview on didpresentSearchcontroller or willpresentSearchcontroller method of iOS 11 SearchController as shown in screenshot. There is some kind of dimming or UISearchView which not allowing me to touch the view controller or scroll the tableview. I think it disables the view.
The greycolor view is not letting me touch the viewcontroller or tableview.
How can i interact with viewcontroller or scroll the tableview.
I want to show result on this view controller only .
// I dont want to show anotherview controller as result
searchController = UISearchController(searchResultsController: nil)
I got it.
Use
let searchController = UISearchcontroller()
func willPresentSearchController(_ searchController: UISearchController) {
searchController.dimsBackgroundDuringPresentation = false
}
Since iOS 12 it is:
searchController.obscuresBackgroundDuringPresentation = false
You should add it before the refresh search.

SearchBar delegate in UISearchController not work

I'm trying to add a search bar to a view, but I've tried it a thousand ways and the searchbar delegate does not work.
class SearchTableViewController: UITableViewController, UISearchBarDelegate{
override func viewDidLoad() {
super.viewDidLoad()
let searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = true
searchController.obscuresBackgroundDuringPresentation = true
self.definesPresentationContext = true
searchController.searchBar.delegate = self
self.tableView.tableHeaderView = searchController.searchBar
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
I ran into the same problem.
The issue you are having is that UITableViewController has a big restriction: its main view must be a UITableView that takes up the entire screen space (except for a possible navigation bar at the top, and a toolbar or tab bar at the bottom).
If your screen consists of just a UITableView, then it makes sense to use it. However, since you are adding a search bar, you must use a UIViewController and add a UITableView to it. Then you can add the search bar as you like.

UISearchbar (UISearchResultsUpdating) with Segue is still visible

I have a problem with a UISearchBar. When ill search some Text in combination with an UITableView, and ill click on one result Cell, the UISearchBar is still visible in the next View Controller. If ill go back (with Segues) - the UISearchbar is still there (with the Keyword)
So after ill click on one result, ill get (in the next View Controller):
Ill use it this way:
class ...: UITableViewController, UISearchResultsUpdating {
var filterSearchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
filterSearchController.searchResultsUpdater = self
filterSearchController.hidesNavigationBarDuringPresentation = false
filterSearchController.dimsBackgroundDuringPresentation = false
filterSearchController.searchBar.searchBarStyle = .Minimal
filterSearchController.searchBar.sizeToFit()
self.tableView.tableHeaderView = filterSearchController.searchBar
Any ideas what could be a problem?
You need to dismiss the UISearchController yourself before transitioning to the next view controller with:
filterSearchController.active = false

UISearchControllerDelegate - Search bar not visible in table header

My UITableViewController is conforming to the new UISearchControllerDelegate and also UISearchResultsUpdating.
Here is my setup code for the search bar:
override func viewDidLoad() {
var searchController = UISearchController(searchResultsController: self)
searchController.searchResultsUpdater = self
self.tableView.tableHeaderView = searchController.searchBar
self.definesPresentationContext = true
}
However, when running this in the simulator there is no search bar in the table header, even though it is specified in the code. I also tried this code in viewWillAppear, but again no search bar was shown.
I was informed by an Apple Engineer that you must give the Search Bar a frame. If you print the frame of the search bar, you will notice it's height is zero. So this is probably a bug in Apple's code.
searchController.searchBar = CGRectMake(0.0, 0.0, 320.0, 44.0)
Edit:
The documentation specifies that you must pass in the View Controller that you want to display the results. To display this in the same View Controller you are in, pass in nil.
var searchController = UISearchController(searchResultsController: nil)

Resources