The white space only appears on iOS 10.
5 Answers
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? ⬇️"
}
}
...
-
-
4This doesn't help when you want the search controller to have a separate view for the search results. This gap appears in iOS 10, but not iOS 7-9.– MolandaSep 15, 2016 at 0:33
Based on @yishus answer here: https://stackoverflow.com/a/39871273/4405051
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 aUITableViewcontroller
as thesearchResultsController
of aUISearchController
.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) }
I disable vertical scroll indicator but the problem is still there. Also, you can't use #available
to check for iOS version. It is used for checking that an API is available or not in an iOS version. So I ended up using this solution:
In search results controller (not the main view controller):
override func viewDidLoad() {
super.viewDidLoad()
if ProcessInfo().isOperatingSystemAtLeast(OperatingSystemVersion(majorVersion: 10,
minorVersion: 0,
patchVersion: 0)) {
automaticallyAdjustsScrollViewInsets = false
}
}
When update search results in main view controller:
func updateSearchResults(for searchController: UISearchController) {
if ProcessInfo().isOperatingSystemAtLeast(OperatingSystemVersion(majorVersion: 10,
minorVersion: 0,
patchVersion: 0)) {
searchResultsController.tableView.contentInset = UIEdgeInsets(top: topLayoutGuide.length,
left: 0,
bottom: bottomLayoutGuide.length,
right: 0)
}
// Filter results here
}
searchResultsController
is the controller mentioned above.
If you want to handle orientation, set the searchResultsController.tableView.contentInset
again when orientation changed.
Still, there's one more problem, everytime the main view controller appear (switch from another tab bar, pop a view controller,...), updateSearchResults
is called. It's pretty bad for performance since I load results asynchronously.
I tried a different approach that seems to have worked for me. For your app's main target, set "Hide Status Bar" to true. At least for me (Xcode 8 GM seed) this actually did not hide the status bar within the app, but seems to have corrected the spacing issue.
Whereas this fixed the issue for me when the phone is vertically oriented, it did not entirely resolve the spacing issue when horizontally oriented. This bit of code also fixes the issue on horizontally aligned screens:
override var prefersStatusBarHidden: Bool {
get {
return UIApplication.shared.isStatusBarHidden
}
}
Of course, setting the prefersStatusBarHidden value to false will entirely disable the status bar, but that may not be a feasible workaround for some developers.
Hope this helps. This is a bit of a hack and I hope Apple resolves this in a future iOS update.
Finally, I found the easiest solution:
automaticallyAdjustsScrollViewInsets = false
🎉😃
-
I added automaticallyAdjustsScrollViewInsets=false to both the searchController and the searchResultsController and still have the gap with iOS 10.– MolandaSep 15, 2016 at 0:37