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

戏子无情 提交于 2019-12-06 06:11:12

问题


The white space only appears on iOS 10.


回答1:


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)
}



回答2:


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




回答3:


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 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)
}

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.




回答4:


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.




回答5:


Finally, I found the easiest solution:

automaticallyAdjustsScrollViewInsets = false

🎉😃



来源:https://stackoverflow.com/questions/39318740/strange-white-space-at-uitableview-header-when-using-uisearchcontroller-with-uit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!