Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange white space at UITableView header when using UISearchController with UITableViewController on iOS 10

The white space only appears on iOS 10.

iOS10HeaderBug

like image 360
alexruperez Avatar asked Dec 15 '22 03:12

alexruperez


2 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)
}
like image 103
yishus Avatar answered May 30 '23 13:05

yishus


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? ⬇️"
    }
}
...

No more whitespace!

like image 25
PabloLerma Avatar answered May 30 '23 11:05

PabloLerma