9

I have a tableview with a search bar. The search bar is provided by a UISearchController. When I add the search bar to the header view of the table, the first row of the table gets covered by the search bar.

How do I prevent the search bar from hiding the first row?

I have this snippet in viewDidLoad:

self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchResultsUpdater = self
self.searchController.searchBar.delegate = self     
self.tableView.tableHeaderView = self.searchController.searchBar
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.searchBar.sizeToFit()
1
  • 1
    if you activate and then cancel the search bar, does it work correctly after?
    – Zsolt
    Jun 20, 2015 at 11:23

2 Answers 2

9

It seems that you have to explicitly set the scope button titles array if you don't have scope button titles.

self.searchController.searchBar.scopeButtonTitles = [NSArray array];
2
  • 5
    As an additional comment, you must set this array before you set it as a header or any calculations (such as sizeToFit) are taken into account.
    – olivaresF
    Aug 18, 2015 at 2:55
  • This was necessary for iOS 8, I did not spot the layout problem in iOS 9. However this solves the issue. Jul 27, 2016 at 8:24
0

Found this issue to be a layout constraints issue. Resolved by dropping all of my constraints in the view containing my searchcontroller and adding back in individually until I found the offending constraint. Using Xcode 7.1

1
  • 1
    What specifically was the constraint that you had issues with? May 12, 2018 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.