How to stop search bar from hiding on scroll - ios

I have a UINavigationController that has a UISearchController assigned to the navigationItem's searchController. Inside the view I have a UICollectionView. The search bar displays in the navigation bar, but when I scroll, the navigation bar automatically hides. Can I stop this from happening?
let searchController = UISearchController(searchResultsController: recentSearchesViewController)
searchController.searchResultsUpdater = recentSearchesViewController
searchController.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
navigationItem.searchController = searchController

You have to set :
navigationItem.hidesSearchBarWhenScrolling = false

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
}

UISearchBar disappears from NavigationItem after pushing a view controller

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?

Navigation bar Searchbar Space Is not Hiding in Swift 4.2

HI i have implemented Searchbar in Navigationbar and
Hiding Navigationbar Progamatically but the space is not removing
PLease help how can i remove the space of searchbar hiding space
let search = UISearchController(searchResultsController: nil)
search.searchResultsUpdater = self
search.obscuresBackgroundDuringPresentation = false
search.hidesNavigationBarDuringPresentation = false;
search.searchBar.placeholder = "search..."
self.definesPresentationContext = true
self.navigationItem.searchController = search
for Hiding the SearchBar
search.searchBar.isHidden = false
Hiding it won't be enough because it is still the navigationItem searchController, so you need to set it to nil
self.navigationItem.searchController = nil
and later restore it if you want, simple as that.

Custom UITableViewController for UISearchController overlays the search bar in iOS 11

Custom UITableViewController for UISearchController overlays the search bar in iOS 11 when you set the search controller to the navigationItem.searchController. The table view controller covers the whole screen so you cannot see the search bar while typing. This is not an issue when you send nil to UISearchController. Basically, I have a search bar for this map app, so I am setting a UITableViewController to the UISearchController to handle the display of the search result when searching for location. Previously, the table view shows up at the bottom of the search bar, now it covers the whole screen.
Here's a code snippet:
searchController = UISearchController(searchResultsController: searchResultsController)
searchController.searchBar.sizeToFit()
searchController.hidesNavigationBarDuringPresentation = true
searchController.dimsBackgroundDuringPresentation = true
searchController.definesPresentationContext = false
searchController.searchResultsUpdater = self
searchController.delegate = self
if #available(iOS 11.0, *){
self.navigationItem.searchController = searchController
}
Found the issue. Need to set the right origin value in willPresentSearchController

Why is UISearchController search bar incorrectly positioned?

I have a UITableViewController where I have placed a search bar in the tableHeaderView. It all looks fine like the picture below.
But, the problem is when it is presented:
As you can see, it is mostly hidden above the top of the screen.
Here is the code I used to set it up in the tableview controller's viewDidLoad:
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Presets..."
searchController.searchBar.delegate = self
searchController.searchBar.barStyle = .blackTranslucent
searchController.hidesNavigationBarDuringPresentation = true;
self.tableView.tableHeaderView = searchController.searchBar
self.tableView.contentOffset = CGPoint(x:0, y:searchController.searchBar.frame.size.height);
If I print the frame of the searchbar, it's origin is (0,0).
I have tried everything I can think of, but nothing works
I figured out that if I check Translucent in the Navigation Bar's Attributes Inspector in IB the issue is resolved.
This leads me to believe that leaving that unchecked and setting extendedLayoutIncludesOpaqueBars to true would also work, but I found this not to be the case.

Resources