I am pushing a viewController where I want a searchBar, but search bar is not showing at all. Below is the code. Am I missing something?
var searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search here..."
definesPresentationContext = true
searchController.searchBar.delegate = self
searchController.searchBar.sizeToFit()
if #available(iOS 11.0, *) {
self.navigationItem.searchController = searchController
} else {
// Fallback on earlier versions
navigationItem.titleView = searchController.searchBar
navigationItem.titleView?.layoutSubviews()
}
You need to add this line to your code:
navigationItem.hidesSearchBarWhenScrolling = false
That removes hiding searchBar while scrolling and shows it on pushing your view controller.
So, navigationItem.hidesSearchWhenScrolling only works when you set the searchController property of navigationItem NOT when you set the navigationItem.titleView to searchBar.
Related
I'm trying to show the UISearchController by tapping on the UIBarButtonItem. But the SearchBar appears behind the NavigationBar. If I set definesPresentationContext = false, then the SearchBar appears above the NavigationBar, but the transitions don't work.
#IBAction func searchButtonPressed(_ sender: Any) {
searchResultsController = self.storyboard?.instantiateViewController(withIdentifier: "SearchResultsController") as? SearchResultsController
searchResultsController.tableView.delegate = self
let searchController = UISearchController(searchResultsController: searchResultsController)
searchController.searchResultsUpdater = searchResultsController
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.autocapitalizationType = .words
definesPresentationContext = true
present(searchController, animated: true)
}
Before BarButtonItem tapped
After BarButtonItem tapped
View UI Hierarchy
Interface Builder
searchController.hidesNavigationBarDuringPresentation = false solve this problem, but I need the navigation bar to not be hidden
UPDATE:
Inserting the SearchBar inside the Navigation Bar is not suitable for my app
https://imgur.com/a/74e5sSk
If you are using UITableViewController then you can do the following:
let searchController = UISearchController(searchResultsController: nil)
searchController.obscuresBackgroundDuringPresentation = false
searchController.definesPresentationContext = true
Then, you need to assign this searchController to navigationItem:
self.navigationItem.searchController = searchController
Now searchbar is going to appear when you pull the tableview and will look this way:
If you want to keep the search bar permanently, then do this:
self.navigationItem.hidesSearchBarWhenScrolling = false
Maybe this is impossible, but I always assumed you could just throw a UISearchController instance onto any old view controller's navigationItem and get a search bar. It seems to me like no matter what I try, I can't get it to work. It's making me think this behavior is hardcoded to only work when the view controller's view property is a subclass of UIScrollView.
I hope this is just a red herring. If I missed something obvious, please help! This is infuriating.
Here's what I did:
import UIKit.UIViewController
class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.hidesSearchBarWhenScrolling = false
navigationItem.searchController = {
let searchController = UISearchController()
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.obscuresBackgroundDuringPresentation = false
return searchController
}()
}
}
No search bar ever appears on screen. It just looks like a regular old navigation bar.
The UISearchController initializer should be let searchController = UISearchController(searchResultsController: nil) or replace nil with a seperate controller to display the search results.
If your viewController is in a UINavigationController stack then the above code should work (with the corrected initializer). Otherwise you will need to create a UINavigationBar and add it to the view. Then add the searchController.searchBar to the navigationItem.titleView
let navigationBar = UINavigationBar()
view.addSubview(navigationBar)
navigationBar.barTintColor = UIColor.gray
navigationBar.translatesAutoresizingMaskIntoConstraints = false
navigationBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
navigationBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
navigationBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
navigationBar.delegate = self
navigationBar.items = [navigationItem]
navigationItem.searchController = {
let searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.obscuresBackgroundDuringPresentation = false
return searchController
}()
navigationItem.titleView = navigationItem.searchController?.searchBar
override func viewDidLoad() {
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
self.tableView?.tableHeaderView = searchController.searchBar
}
This is default behavior when you add the search bar to tableview header.
I have a UITableViewController that shows countries group by the alphabet. I added a UISearchController in code:
let searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.placeholder = ""
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
The problem is that when I dismiss the search bar, the UI visual kind of breaks, showing a strange space above the search bar:
Any ideas why this happens and how to solve it?
Try adding:
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.sizeToFit()
self.definesPresentationContext = true
I have a problem regarding the UISearchController component. I have set up a searchcontroller like this:
let searchResultsController = TSSearchResultsController(nibName: "TSSearchResultsController", bundle: nil)
searchResultsController.delegate = self
let searchController = UISearchController(searchResultsController: searchResultsController)
searchController.delegate = self
// SearchController
searchController.searchResultsUpdater = searchResultsController
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
// SearchBar
searchController.searchBar.delegate = searchResultsController
searchController.searchBar.placeholder = NSLocalizedString("Search", comment: "")
searchController.searchBar.tintColor = IDAssetManager.getColorWhite()
searchController.searchBar.sizeToFit()
searchController.searchBar.searchBarStyle = .minimal
searchController.searchBar.showsCancelButton = false
self.definesPresentationContext = true
...
When I click the search result I push a view onto the navigationstack which is defined in the same place I initialise the searchController. When I come back, the searchController view has been pushed down a few a bit. This is continuously happening every time I push and pop from navigationController. I have tried self.extendedEdgesForLayout = [] but does not help.