I've implemented UISearchController and presenting it on UIViewController.
Navigation bar has search button and on clicking search, UISearchController will present from top of the screen.
Issue is, In iPhone X it looks fine but in all other devices it doesn't fit properly with spacing.
My implementation is like this :
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.delegate = self
searchController.searchBar.tintColor = UIColor.white
searchController.searchBar.sizeToFit()
searchController.searchBar.placeholder = SEARCH
searchController.searchBar.barTintColor = UIColor(hex: 0x2A3442)
searchController.searchBar.becomeFirstResponder()
// Search bar UI change
for subview in searchController.searchBar.subviews {
for innerSubview in subview.subviews {
if innerSubview is UITextField {
let textField = innerSubview as? UITextField
textField?.textColor = UIColor.white
textField?.borderStyle = .roundedRect
textField?.backgroundColor = UIColor(hex: 0x38465A)
}
}
}
present(searchController, animated: true)
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 have UISearchController in my app:
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
if #available(iOS 9.1, *) {
searchController.obscuresBackgroundDuringPresentation = false
}
searchController.searchBar.frame = searchBarView.frame
searchBarView.addSubview(searchController.searchBar)
searchController.searchBar.delegate = self
The problem is that when I enter the SearchBar and then click the cancel button the search bar not back to the frame I define here(and stretched to full size screen):
searchController.searchBar.frame = searchBarView.frame
Any idea what can be the problem?
I have a trouble with a search bar only on iOS 10 (it works well on iOS 11) and I don't know what's going on, it's been several days I'm trying to solve this by myself without success so I hope someone here could tell me what I do wrong.
So, initially, my screen is like this
And when I click in the search bar, it goes like that
If I type text, it well displays the results under the search bar. I just don't know where this space between my search bar and the navigation bar comes from.
The results of my search is managed by an another viewcontroller declared like this.
let locationSearchTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearchTableViewController") as! LocationSearchTableViewController
resultSearchController = UISearchController(searchResultsController: locationSearchTable)
resultSearchController?.searchResultsUpdater = locationSearchTable
A bit of my code, especially where I set up the search bar:
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search for a place or address"
if #available(iOS 11.0, *) {
navigationItem.searchController = resultSearchController
resultSearchController?.dimsBackgroundDuringPresentation = true
} else {
searchBar.delegate = self
searchBar.searchBarStyle = .default
searchBar.setShowsCancelButton(false, animated: true)
searchBar.keyboardAppearance = .default
navigationController?.navigationBar.isTranslucent = false
searchBar.isTranslucent = false
resultSearchController?.dimsBackgroundDuringPresentation = false
mainView.addSubview(searchBar)
}
definesPresentationContext = true
resultSearchController?.hidesNavigationBarDuringPresentation = false
}
For others who might encounter the same issue, here is my code now and it's working.
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search for a place or address"
if #available(iOS 11.0, *) {
navigationItem.searchController = resultSearchController
resultSearchController?.hidesNavigationBarDuringPresentation = false
resultSearchController?.dimsBackgroundDuringPresentation = true
} else {
searchBar.searchBarStyle = .default
searchBar.setShowsCancelButton(false, animated: true)
searchBar.keyboardAppearance = .default
navigationController?.navigationBar.isTranslucent = false
searchBar.barStyle = .default
searchBar.isTranslucent = false
resultSearchController?.hidesNavigationBarDuringPresentation = true
resultSearchController?.dimsBackgroundDuringPresentation = false
UI_mapView.addSubview(searchBar)
}
definesPresentationContext = true
let currentLocationButtonItem = MKUserTrackingBarButtonItem(mapView: UI_mapView)
self.navigationItem.rightBarButtonItem = currentLocationButtonItem
I showed UIViewController like modal style. UIViewController has UITableView. I added UISearchController to UITableView but UISearchController has incorrect behavior when it disappeared. I made many times UISearchControllers but I didn't face the this behavior.
My code
So I show the UIViewController
#objc fileprivate func selectInterlocutor(_ button: UIButton) {
let selectInterlocutorTVC = SelectInterlocutorTableViewController()
selectInterlocutorTVC.modalPresentationStyle = .overCurrentContext
selectInterlocutorTVC.modalTransitionStyle = .coverVertical
selectInterlocutorTVC.providesPresentationContextTransitionStyle = true
present(selectInterlocutorTVC, animated: true) {
}
}
I add UISearchController to tableView
fileprivate func addSearchController() {
searchController.searchBar.barStyle = .default
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
searchController.searchBar.autocapitalizationType = .words
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.sizeToFit()
tableView.tableHeaderView = searchController.searchBar
}
Please look at this video that you can see this behavior.
https://www.dropbox.com/s/l3m3q8wmqoy3qv2/SearchBarBug.mov?dl=0
How can I fix it?
I removed UISearchController and I used UISearchBar and it works for me.
fileprivate func addSearchController() {
searchBar.barStyle = .default
searchBar.delegate = self
searchBar.autocapitalizationType = .words
searchBar.showsCancelButton = true
searchBar.sizeToFit()
tableView.tableHeaderView = searchBar
}
In use UISearchController, try to modify the searchBar barTintColor attributes, change color.Results presented two black line, the effect such as the following picture.
how can I remove the two black line?
my code:
// 搜索结果控制器
searchVC = SearchViewController()
searchController = UISearchController(searchResultsController: searchVC)
// 设置UISearchController属性
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = true
// 设置UISearchBar属性
searchController.searchBar.delegate = self
searchController.searchBar.sizeToFit() //直接关系到searchBar会不会上推
searchController.searchBar.barTintColor = BackgroudGray
searchController.searchBar.tintColor = UIColor.yellowColor()
// 设置本ViewController
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
try this one
searchController.searchBar.backgroundImage = UIImage()
searchController.searchBar.backgroundColor = UIColor.lightGray