I have added SearchController's searchbar to tableview controller's headerview but when I tap on the searchbar keyboard doesn't pop up. it feels like there is no user interaction. Following is the code used to add searchbar to uitableview.
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.userInteractionEnabled = true
dispatch_async(dispatch_get_main_queue(),{
self.tableView.tableHeaderView = controller.searchBar
controller.searchBar.bounds = (self.tableView.tableHeaderView?.bounds)!
controller.definesPresentationContext = true
self.definesPresentationContext = true
})
return controller
})()
Try to add just this 5 lines of code in your viewDidLoad and give it a try
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
self.tableView.tableHeaderView = searchController.searchBar
Related
I have tableViewController at the storyboard.
I programmatically added searchController to the table header.
private func setupSearchView(){
if !showSearchBar { return }
let storyboard = UIStoryboard(name: "Search", bundle: nil)
resultViewController =
storyboard.instantiateViewController(withIdentifier: "NewSearchTableViewController") as? SearchResultTableViewController
searchController = UISearchController(searchResultsController: resultViewController)
resultViewController?.tableView.delegate = self
searchController.delegate = self
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self // Monitor when the search button is tapped.
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
tableView.tableHeaderView = searchController.searchBar
searchController.searchBar.sizeToFit()
searchController.searchBar.placeholder = "Search here...".localized
definesPresentationContext = true
}
When I selected the cell in the table and went to the details screen - it works perfectly.
But when I pressed button back, the search bar was scrolled under the navigation bar.
Normal behaiver
Bad behavior after button back pressed
I've tried to add a simple search controller but whenever I'm clicking on it so it becomes active, it jumps up, out of screen. Why is that even happening? I checked all the code and I'm not manipulating with constraints or anything.
I initialize my search controller through the following function (and call it in ViewDidLoad):
private func initSearchController() {
self.searchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.searchBar.delegate = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.barStyle = UIBarStyle.black
controller.searchBar.barTintColor = UIColor.white
controller.searchBar.backgroundColor = UIColor.clear
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
self.tableView.reloadData()
}
self.searchController.hidesNavigationBarDuringPresentation = false
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.
in my swift 2 app i have a table view with a search bar:
But if tap on the search bar, my navigation bar and the search bar will be hidden.
This is my code which is in the viewDidLoad
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.hidesNavigationBarDuringPresentation = true
self.MyTable.tableHeaderView = controller.searchBar
return controller
})()
At the beginning i also get this message:
Attempting to load the view of a view controller while it is
deallocating is not allowed and may result in undefined behavior
()
My Question is, where is my mistake? :/
from this tutorial you can use this code to solve the problem.
self.navigationController!.navigationBar.translucent = false
searchController!.hidesNavigationBarDuringPresentation = false
// This makes the view area include the nav bar even though it is opaque.
// Adjust the view placement down.
self.extendedLayoutIncludesOpaqueBars = true
self.edgesForExtendedLayout = UIRectEdge.Top
You have to modify your code with the below if you want to make it worked perfectly:
self.resultSearchController = UISearchController(searchResultsController: nil)
self.resultSearchController.searchResultsUpdater = self
self.resultSearchController.searchBar.delegate = self
self.definesPresentationContext = true
self.resultSearchController.dimsBackgroundDuringPresentation = false
self.resultSearchController.hidesNavigationBarDuringPresentation = true
if #available(iOS 11.0, *) {
self.navigationItem.searchController = self.resultSearchController
} else {
self.tableView.tableHeaderView = self.resultSearchController.searchBar
}
I put my UISearchController's search bar on navigation bar's titleView. But when I tap to search, UISearchBar does not respond. What can be the cause ?
let tvController:UITableViewController = UITableViewController()
tvController.tableView.delegate = self
tvController.tableView.dataSource = self
self.searchController = UISearchController(searchResultsController: tvController)
self.searchController?.searchResultsUpdater = self
self.searchController!.searchBar.placeholder = "Search for places"
self.searchController!.hidesNavigationBarDuringPresentation = false
self.searchController!.dimsBackgroundDuringPresentation = false;
self.searchController!.searchBar.searchBarStyle = UISearchBarStyle.Prominent
self.searchController!.delegate = self
self.searchController!.searchBar.delegate = self
self.definesPresentationContext = true
self.searchController!.searchBar.sizeToFit()
self.navigationItem.titleView = self.searchController!.searchBar
This might help
Replace
self.searchController = UISearchController(searchResultsController: tvController)
With
self.searchController = UISearchController(searchResultsController: nil)