UISearchBar disappears from NavigationItem after pushing a view controller - ios

I have a UISearchBar embedded in the NavigationItem, like so:
let search = UISearchController(searchResultsController: nil)
search.searchBar.delegate = self
search.hidesNavigationBarDuringPresentation = false
search.definesPresentationContext = false
search.delegate = self
navigationItem.searchController = search
And I also have a collectionView.
When I push a viewController from the collectionview, and then go back, I can't see the UISearchBar anymore.
Any way of fixing this?

Related

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

Add UISearchBar to UINavigationBar with UIBarButtonItem

In my app, I used to have a search bar in the header view of my table view. However, I have added a search bar button item, and when a user taps it, I want a search bar to animate across the navigation bar, and the user should be able to search. This is kind of like the search bar in the Twitter iOS app. here is the code for my UISearchController:
self.searchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.hidesNavigationBarDuringPresentation = true
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
//self.tableView.tableHeaderView = controller.searchBar
self.definesPresentationContext = true
controller.searchBar.returnKeyType = .Search
controller.searchBar.delegate = self
return controller
})()
Here is how my UINavigationBar looks like right now:
How would I go about doing this?
You're almost there. You need to do four things:
searchController.hidesNavigationBarDuringPresentation = false
navigationItem.titleView = searchController.searchBar
hide all the current UIBarButtonItems you have in the navigation bar
searchController.searchBar.becomesFirstResponder()
That is, you setup your UISearchController so it doesn't hide the navigation bar, as the UISearchBar is gonna be displayed there.
Then, when your IBAction runs, you do something like:
navigationItem.hidesBackButton = true
navigationItem.titleView = searchController.searchBar
navigationItem.rightBarButtonItem = nil
searchController.searchBar.becomeFirstResponder()
That guarantees the UISearchBar will use the entire UINavigationBar when it becomes the first responder. You also get the UISearchBar animation, which does a good job in hiding the unanimated removal of the UIBarButtonItems.
You will need to implement the UISearchBarDelegate (or if you prefer UISearchControllerDelegate) to restore the original navigation bar state once the user is done with the search.
One very important detail is that in order for your UISearchBar to be editable in the titleView, no other view controller can be set with definesPresentationContext = true at the time yours is pushed.

How do I set the UISearchController's searchBar to a view that isn't the tableHeaderView or navigationItem.titleview?

I'm trying to keep the search bar in view as the table scrolls. At the moment I'm placing it as the header in a tableview, and it works as it should, but of course the search bar scrolls off screen as you go down the table. I thought I could do this simply modifying this code sample:
How do I use UISearchController in iOS 8 where the UISearchBar is in my navigation bar and has scope buttons?
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
tableview.tableHeaderView = searchContoller.searchBar // How to put it elsewhere?
//Alternative that also works
navigationItem.titleView = searchController.searchBar
The idea was to take some other view and do
otherview = searchController.searchBar
For instance an outlet to a UISearchBar, or a blank UIView, or something like that.
But it doesn't show the searchBar if I do that. It seems to only work as the header view of a table or as a navigationItem.titleView.
Am I missing something?
If you have a blank UIView that is placed above the tableview.
let's assume you have an outlet to that blank UIView called searchContainer.
Then you can add the search bar of the UISearchController to that view by adding the following line
searchContainer.addSubview(searchController.searchBar)

UISearchBar in UISearchController Does Not Disappear

I AM NOT USING STORYBOARD SEGUES.
When I use UISearchController to have a search bar at the top of my table view, I get extremely strange behavior. Is there documentation on how I'm supposed to handle the search bar when a view dismisses? When I switch to a new view controller via an animation or push one on a nav stack, the bar is stuck in its spot until I hit the "Cancel" button.
See a video of what's happening in this short clip:
https://www.youtube.com/watch?v=6G7xFMENm_o&feature=youtu.be
This is the code, in the view controller, that sets up search bar:
var s: UISearchController!
private func configureSearching() {
s = UISearchController(searchResultsController: nil)
s.searchResultsUpdater = self
s.dimsBackgroundDuringPresentation = false
s.searchBar.searchBarStyle = .Minimal
s.hidesNavigationBarDuringPresentation = false
tableView.tableHeaderView = s.searchBar
s.searchBar.sizeToFit()
}
you should add the following line in viewDidLoad
self.definesPresentationContext = true

The navigation bar disappears in the search result table view

I am trying to have a UISearchController in the navigation bar and display the results in an external controller.
For some reason the navigation bar disappear as soon as I type something in
I have been trying different solutions for a few hours with no results. It looks like it is a similar issue as Navigation bar disappears when typing in UISearchController text field and
Navigation bar disappears if reload data with UISearchController that did not get any answer.
self.cearchController = ({
//creating another tableview
let storyBoard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let alternateController:SearchResultsTableViewController = storyBoard.instantiateViewControllerWithIdentifier("SearchResultsTableViewController") as! SearchResultsTableViewController
let controller = UISearchController(searchResultsController: alternateController)
controller.hidesNavigationBarDuringPresentation = false
controller.dimsBackgroundDuringPresentation = false
controller.searchResultsUpdater = alternateController
controller.searchBar.sizeToFit()
controller.searchBar.placeholder = "Search"
self.navigationItem.titleView = controller.searchBar
return controller
})()
I have tried
self.navigationController?.setNavigationBarHidden(true, animated: false)
and I have
myResultsTableView.definesPresentationContext = true
in the viewdidload
this is what it looks like :
Note: I have only started with swift a few days ago so I might be missing something really obvious!!
Thanks and happy to add more code
So I had a similar issue of the navbar disappearing when my search results were shown. But controller.hidesNavigationBarDuringPresentation = false did the trick for me.
Maybe try using tableView.tableHeaderView = searchController.searchBar instead of self.navigationItem.titleView = controller.searchBar

Resources