UISearchBar doesn't dismiss on push segue - ios

I'm trying out UISearchController for iOS 8 right now. When I click the cell, it will push the segue and show another view controller. However, the search controller/bar is still there on the next controller. Also, I notice that the status bar background is white, while it should be grey as the searchBar background color is grey. Is there anything that I miss?
This is the codes that I used to initialise the search controller
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.sizeToFit()
tableView.tableHeaderView = searchController.searchBar
}
Screenshot:

Manual stop UISearchController in prepareForSegue
searchController.active = false
Or add this in viewDidLoad
searchController.definesPresentationContext = true

Related

Searchbar become hidden when navigating back

I am showing searchbar in home page. When I push a UIViewController then get back to home page, the searchbar become hidden.
here is my code
func setupSearch(){
searchController = UISearchController(searchResultsController: resultsTableController)
searchController.searchResultsUpdater = self
searchController.searchBar.autocapitalizationType = .none
searchController.searchBar.searchTextField.placeholder = "Enter a search term...".localized()
searchController.searchBar.returnKeyType = .search
// Place the search bar in the navigation bar.
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = true
// searchController.hidesNavigationBarDuringPresentation = false
// Monitor when the search controller is presented and dismissed.
searchController.delegate = self
// Monitor when the search button is tapped, and start/end editing.
searchController.searchBar.searchTextField.delegate = self
/** Specify that this view controller determines how the search controller is presented.
The search controller should be presented modally and match the physical size of this view controller.
*/
definesPresentationContext = true
}

What is this strange white space at the top of my UIViewController when using the Search Controller on my device?

Left side displays my app in the iPhone 7 simulator. Right side is the app as displayed in my device. When tapping in the search bar, the navbar sildes up but then slides back down as showing in the image with the "What is this?" text.
The code for my search controller is as follows and it is being called in viewDidLoad.
//MARK: - Search Controller
func configureSearchController() {
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
// to receive updates
searchController.searchResultsUpdater = self
// Set up searchbar of search controller
searchController.searchBar.scopeButtonTitles = ["All", "Something", "Something"]
searchController.searchBar.delegate = self
// add the searchbar to the tableview
tableView.tableHeaderView = searchController.searchBar
// hack to fix ocassional bugs
searchController.searchBar.sizeToFit()
}

UISearchController woes

I'm having an issue with UISearchController. There have been some reports of erratic behavior in iOS 11, but none of the suggestions have fixed my problem.
The navigation bar is hidden in my app so I just want to place the search bar between two buttons on the main screen. I put UIView in the storyboard to serve as the superview for the search bar. When activated the results controller is a straight UITableViewController.
Everything is in-place when the app launches. When I access the search bar it just to the top of the screen, leaving it's parent view behind. Everything functions ok, but when I hide the table view, the search bar actual goes a bit lower than it was when it started. Here's the setup code:
let locationSearchTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearchTable") as! LocationSearchTable
searchController = UISearchController(searchResultsController: locationSearchTable)
searchController.searchResultsUpdater = locationSearchTable
searchController.delegate = self
locationSearchTable.mapView = mapView
locationSearchTable.handleMapSearchDelegate = self
locationTableController = locationSearchTable
let searchBar = searchController!.searchBar
//searchBar.searchBarStyle = UISearchBarStyle.prominent
searchBar.sizeToFit()
searchBar.placeholder = "Search..."
searchViewHolder.addSubview(searchBar)
searchBar.frame = searchViewHolder.bounds
searchBar.autoresizingMask = [.flexibleWidth, .flexibleHeight]
searchController.dimsBackgroundDuringPresentation = true
definesPresentationContext = false
I have tried many, many approaches to fixing this. I'm wondering if I misunderstood an earlier suggestion. Any advice is welcome.
I'd the same issue. If your navbar is not translucent try to put this in viewDidLoad:
extendedLayoutIncludesOpaqueBars = true

iOS11 SearchController in NavigationBar with Scope Buttons

In iOS 11 you can put a UISearchController in the NavigationBar with a few lines of code.
I set up everything in the ViewController.swift.
func setupNavBar() {
navigationController?.navigationBar.prefersLargeTitles = true
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = wordViewController
searchController.searchBar.scopeButtonTitles = ["French", "English"]
searchController.searchBar.delegate = wordViewController
navigationItem.searchController = searchController
// Make searchbar persistent
navigationItem.hidesSearchBarWhenScrolling = false
}
In my delegate, the search fires and filters properly. However, if I click either of the scope buttons, they simply disappear. This delegate method is never called. (filter by scope is not actually implemented yet)
extension WordViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
if let searchText = searchBar.text {
print("Scoped changed: \(searchText)")
filteredWordList = wordList.filter({$0.contains(searchText)})
}
}
}
Full source is on Github:
https://github.com/melling/ios_topics/tree/master/NavBarSearch
https://github.com/melling/ios_topics/tree/master/NavBarSearch/NavBarSearch
Try to add this code within setupNavBar():
searchController.dimsBackgroundDuringPresentation = false
I was able to use Artem's solution,
searchController.dimsBackgroundDuringPresentation = false
but I then ran into the problem of no longer being able to click on the background to dismiss the focus on the search bar.
To resolve this, I created my own dimmer uiview (screen size, background color of black with 25% opacity), and added that to the view of the currently presented screen, and listened for a tap. When the user taps it, I fade it out and set
searchController.active = false
In your ViewController, you need to add this line
self.definesPresentationContext = YES;
This tells the UISearchController the presentation context it needs to use, which is defined by your ViewController.
Check this link for more details: http://asciiwwdc.com/2014/sessions/228

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.

Resources