UISearchController view keeps getting pushed down when push-popping controllers from searchResultsController - ios

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.

Related

Why searchController nested in the tableView scrolled under the navigationBar?

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

Search bar not showing up on the navigation bar iOS 11

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.

Cannot Interact with SearchBar added as HeaderView

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

Dismissing UISearchController breaks grouped UITableViewController visual

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

'cancel' button of searchbar disappear in iPad

My app needs support universal devices.
And in that, there is a view controller which have UISearchController. It can show normally in iPhone device, but in iPad, the 'cancel' button disappear.
iPhone
iPad
It is the relevant code about searchbar in the view controller, following.
func configureSearchController() {
self.collectionView!.updateConstraints()
resultsTableController = SearchBookResultTableViewController()
resultsTableController.tableView.delegate = self
searchController = UISearchController(searchResultsController: resultsTableController)
searchController.searchResultsUpdater = self
navigationItem.titleView = searchController.searchBar
searchController.searchBar.sizeToFit()
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.delegate = self
searchController.searchBar.searchBarStyle = .Minimal
searchController.searchBar.placeholder = "书籍、作者、出版社"
// stress
searchController.searchBar.showsCancelButton = true
definesPresentationContext = true
}
I want to know the reason of the problem, and in which way I can solve it.
Seek help! Thx.
I found the same situation in iOS 7 in this link. And I use the same way to solve my problem. But I don't know why the way, create a new UIView, can solve it? What's the different between them?
self.collectionView!.updateConstraints()
resultsTableController = SearchBookResultTableViewController()
resultsTableController.tableView.delegate = self
searchController = UISearchController(searchResultsController: resultsTableController)
searchController.searchResultsUpdater = self
searchController.searchBar.sizeToFit()
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.delegate = self
searchController.searchBar.searchBarStyle = .Minimal
searchController.searchBar.placeholder = "书籍、作者、出版社"
let SearchView: UIView = UIView(frame: searchController.searchBar.bounds)
SearchView.addSubview(searchController.searchBar)
navigationItem.titleView = SearchView

Resources