Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extra space on top of UISearchController's UITableView

I set up a UISearchController like this:

    searchResultsController = SearchResultsController()

    searchController = UISearchController(searchResultsController: searchResultsController)
    searchController.searchResultsUpdater = self
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.dimsBackgroundDuringPresentation = true
    searchController.searchBar.searchBarStyle = .minimal
    searchController.delegate = self
    definesPresentationContext = true

    navigationItem.titleView = searchController.searchBar

SearchResultsController is just a view controller with UITableView to show search results.

When the device is in portrait, start searching in portrait mode, there is an extra space on top of the table view (the RESULT header is the start of table view)

Device in portrait - Start search in portrait

Then if I rotate to landscape mode, the space disappeared:

Device in landscape - Start search in portrait

When device is in landscape mode and I start search, then the table view is cut off:

Device in landscape - Start search in landscape

Then if I rotate to portrait, the table view fits perfectly:

Device in portrait - Start search in landscape

The extra space height is exactly same as status bar height. So I wonder there is something related to the status bar here.

This didn't happen if I assign the searchbar as table view's tableHeaderView. It only happens when searchbar is in navigation title. Anyone know why?

like image 725
kientux Avatar asked Jan 05 '23 02:01

kientux


1 Answers

ApparentlyautomaticallyAdjustsScrollViewInsets is getting deprecated.

'automaticallyAdjustsScrollViewInsets' was deprecated in iOS 11.0: Use UIScrollView's contentInsetAdjustmentBehavior instead

So I would suggest doing something like:

searchResultsController.tableView.contentInsetAdjustmentBehavior = .never

It worked like a charm for me. Some useful documentation:

contentInsetAdjustmentBehavior

UIScrollViewContentInsetAdjustmentBehavior

like image 58
inigo333 Avatar answered Jan 07 '23 15:01

inigo333