I have a UISearchController added onto my view, but I would like to change the height of it. This is what I have:
func initSearch() {
// Create the search controller and make it perform the results updating.
searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.hidesNavigationBarDuringPresentation = true
searchController.dimsBackgroundDuringPresentation = true
searchController.searchBar.tintColor = UIColor.whiteColor()
searchController.searchBar.barStyle = .Black
searchController.searchBar.scopeButtonTitles = [ "Any", "Title", "Content", "Keywords" ]
self.definesPresentationContext = true
}
#IBAction func searchTapped(sender: AnyObject) {
// Present the view controller.
presentViewController(searchController, animated: true, completion: nil)
}
Below is a screenshot of what it looks like. I would like trim some space from the height though. How can this be done?
Related
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'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)
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
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
}
i'm trying to show a searchController on buttonClick. However it seem to create an error when the button is clicked. I get an error on the presentViewController row. What am i doing wrong in order to achieve this?
by the way i'm having this code in a tableViewController
func searchButtonClicked(sender: UIBarButtonItem) {
searchController = UISearchController(searchResultsController: self)
// searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
self.definesPresentationContext = true
self.presentViewController(self.searchController, animated: true, completion: nil)
}