The white space only appears on iOS 10.
I ran into this problem as well. If you have the vertical scroll indicator enabled, you should be able to see that it's a UIScrollView
's inset issue. And seems like it only happens when you use a UITableViewcontroller
as the searchResultsController
of a UISearchController
.
And this extra space is visible at both the top and bottom of the view.
This answer is not pretty, but I'm adding this in for now.
if #available(iOS 10.0, *) {
automaticallyAdjustsScrollViewInsets = false
tableView.contentInset = UIEdgeInsetsMake(64, 0, 44, 0)
}
You're setting a UITableViewController as the UISearchController's searchResultsController but without Autolayout nor a frame.
As you can read in the UISearchController's Quick Help, you can pass it nil if you want to display the search results in the same view controller that displays your searchable content.
So you're code will look okay if you set it like this:
class ViewController: UIViewController {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
let tableView = UITableView(frame: view.bounds, style: .plain)
tableView.dataSource = self
view.addSubview(tableView)
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
navigationItem.titleView = searchController.searchBar
searchController.searchBar.becomeFirstResponder()
searchController.searchBar.text = "⬇️ What is this white space? ⬇️"
}
}
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With