SearchController Hidden automatically - ios

I try to show searchController in UIViewcontroller.
This works fine but when I try to write something then search controller is not visible.
My code is as follow:
class tempVC: UIViewController,UISearchBarDelegate,UISearchResultsUpdating {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
searchController.searchBar.placeholder = "Enter name here..."
definesPresentationContext = true
searchController.searchBar.sizeToFit()
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation=false
//navigationItem.titleView = searchController.searchBar
searchController.searchBar.frame=CGRectMake(0, 64, 320, 44)
self.view.addSubview(searchController.searchBar)
}
// MARK: - UISearchResultsUpdating Delegate
func updateSearchResultsForSearchController(searchController: UISearchController) {
// let searchBar = searchController.searchBar
//let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
//filterContentForSearchText(searchController.searchBar.text!, scope: scope)
print("update")
}
}
Please help me to solve this.
Thank you,

class tempVC: UIViewController,UISearchBarDelegate,UISearchResultsUpdating {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
searchController.searchBar.placeholder = "Enter name here..."
definesPresentationContext = true
searchController.searchBar.sizeToFit()
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation=false
//navigationItem.titleView = searchController.searchBar
searchController.searchBar.frame=CGRectMake(0, 64, 320, 44)
self.view.addSubview(searchController.searchBar)
}
// MARK: - UISearchResultsUpdating Delegate
func updateSearchResultsForSearchController(searchController: UISearchController) {
// let searchBar = searchController.searchBar
//let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
//filterContentForSearchText(searchController.searchBar.text!, scope: scope)
print("update")
}
}

Related

How to focus search bar on barbutton click?

I am trying to autofocus a searchbar when i click on a bar button, but it does not seem to work with becomeFirstResponder, follow the GIF:
I've already tried using becomeFirstResponder and it does not work, this is how i am handling the searchbar:
// MARK: SearchBar Delegate
#IBAction func showSearchBarAction(_ sender: Any) {
let searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
searchController.searchBar.keyboardAppearance = .dark
searchController.dimsBackgroundDuringPresentation = false
navigationItem.searchController = searchController
navigationItem.searchController?.isActive = true
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
navigationItem.searchController?.isActive = false
self.navigationItem.searchController = nil
}
All of it works perfectly, but the autofocus is something i really want and i can't change my design as well
Solution
This code is the solution:
This is how my class declaration looks like:
class YourViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate {
// MARK: SearchBar Delegate
#IBAction func showSearchBarAction(_ sender: Any) {
let searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
searchController.searchBar.keyboardAppearance = .dark
searchController.dimsBackgroundDuringPresentation = false
self.navigationItem.searchController = searchController
self.navigationItem.searchController?.isActive = true
}
func didPresentSearchController(_ searchController: UISearchController) {
DispatchQueue.main.async {
searchController.searchBar.becomeFirstResponder()
}
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
navigationItem.searchController?.isActive = false
self.navigationItem.searchController = nil
}

How do I avoid the weird gap between search bar and the table view while search results displaying?

Check: Screenshot
I tried solutions to all relevant questions. However none of them helped really.
I am getting a weird gap between table view and search bar when search bar shows results.
Help me find out what's wrong with my code:
the view controller subclasses from UITableViewController, UISearchBarDelegate, UISearchResultsUpdating
var searchController = UISearchController()
var resultsController = UITableViewController()
var refreshController = UIRefreshControl()
in override :
configureSearchController()
resultsController.tableView.delegate = self
resultsController.tableView.dataSource = self
refreshController.attributedTitle = NSAttributedString(string: "")
refreshController.addTarget(self, action: #selector(refreshSelector), for: .valueChanged)
tableView.addSubview(refreshController)
searchController.searchBar.delegate = self
automaticallyAdjustsScrollViewInsets = false
definesPresentationContext = true
#objc func refreshSelector()
{
if(!searchLoaded)
{
searchLoaded = true
self.tableView.tableHeaderView = searchController.searchBar
self.navigationItem.rightBarButtonItem = nil
print( "Got ya")
}
refreshController.endRefreshing()
}
func configureSearchController ()
{
searchController = UISearchController(searchResultsController: resultsController)
searchController.searchResultsUpdater = self
searchController.searchBar.layer.borderWidth = 1;
searchController.searchBar.barTintColor = UIColor.searchBarBackgroundGrey()
searchController.searchBar.layer.borderColor = UIColor.searchBarBackgroundGrey().cgColor
self.navigationController?.navigationBar.shadowImage = UIImage()
}
deployment target has been set as 9.3

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.

UISearchController not showing

I am working on Swift 3.1, I want a simple search functionality where the search results will show in a popOver manner below the search bar. This is a native offering and should be available using UISearchController. But even after my repeated trials, I could not achieve this. Am I missing something?
I have mentioned the resultsController. Implemented the required delegates and even the data us fed to it. But still its not shown.
Any help will be greatly appreciated. Thanks.
Code:
func initiateView() {
resultsController = ResultsController()
resultsController.table.dataSource = self
searchController = UISearchController.init(searchResultsController: resultsController)
self.view.addSubview(searchController.searchBar)
searchController.searchResultsUpdater = self
searchController.delegate = self
searchController.dimsBackgroundDuringPresentation = false // default is YES
searchController.searchBar.delegate = self // so we can monitor text changes + others
definesPresentationContext = true
feedTable = UITableView()
feedTable.delegate = self
feedTable.dataSource = self
feedTable.separatorStyle = .singleLineEtched
feedTable.register(UITableViewCell.self, forCellReuseIdentifier: "SomeCell")
self.view.addSubview(feedTable)
feedTable.pinToSuperview([.Left, .Right, .Bottom])
feedTable.pinToSuperviewTop(withInset: 50, priority: 100)
}
func updateSearchResults(for searchController: UISearchController)
{
if let searchString = searchController.searchBar.text{
let resultsController = searchController.searchResultsController as! ResultsController
resultsController.resultsArray = feedArray.filter({$0 .contains(searchString)})
resultsController.table.reloadData()
}
}

UISearchController can't set delegate to self

I'm trying to get a UISearchController working in my navigation bar using swift3
I currently have it so a search icon exists in the top right corner. When you click on the icon, it will show you the UISearchController.
The problem is, when I try to do searchController.delegate = self, I get an error saying
Cannot assign value of type 'ParentViewController' to type 'UISearchControllerDelegate'?
I'm assuming since I can't set the delegate, none of the actions are linked, but I can't figure out why it won't let me?
Here's the code I'd like to work. Am I implementing the protocol wrong and just not seeing it, or missing something entirely?
class ParentViewController: ButtonBarPagerTabStripViewController, UISearchResultsUpdating, UISearchBarDelegate {
var searchBarButtonItem: UIBarButtonItem?
override func viewDidLoad() {
searchBarButtonItem = navigationItem.rightBarButtonItem
super.viewDidLoad()
}
func updateSearchResults(for searchController: UISearchController) {
print("update search results")
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
print("search cancelled")
}
func filterContentForSearchText(searchText: String, scope: String = "All") {
print("Filter: " + searchText)
}
#IBAction func startSearchAction(sender: UIBarButtonItem) {
print("Clicked")
let searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.showsCancelButton = true
searchController.dimsBackgroundDuringPresentation = true
self.navigationItem.titleView = searchController.searchBar
definesPresentationContext = true
navigationItem.setRightBarButton(nil, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Resources