4

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?

kientux
  • 1,722
  • 1
  • 14
  • 31

3 Answers3

9

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

inigo333
  • 2,130
  • 1
  • 27
  • 36
  • i am facing the same problem, but setting `tableView.contentInsetAdjustmentBehavior = .never` not working :( – Ratul Sharker Oct 03 '18 at 10:33
  • it works like a charm in case you use `UITableViewController` as a searchResultUpdater not a `UIViewController` containing a `UITableView` in it. – Ratul Sharker Oct 03 '18 at 13:54
  • 1
    Any solution for TableView in UIViewController? – Alizain Prasla Oct 29 '18 at 10:27
  • 1
    @AlizainPrasla You should use something like this then tableView.contentInsetAdjustmentBehavior = .never tableView.contentInset = UIEdgeInsets(top: 120, left: 0, bottom: 0, right: 0) – Shady Mostafa Sep 11 '19 at 14:39
0

write this in viewDidLoad :

self.automaticallyAdjustsScrollViewInsets = false
Bhadresh Mulsaniya
  • 2,560
  • 1
  • 10
  • 24
  • This is `true` by default and I didn't touch this. I want to use transparent navigation bar and tab bar so I have to keep this `true` – kientux Nov 05 '16 at 11:29
0

This problem can appear only for iOS 10. Write this in viewDidLoad

if #available(iOS 10.0, *) {
automaticallyAdjustsScrollViewInsets = false
tableView.contentInset = UIEdgeInsetsMake(64, 0, 44, 0)}