0

I have used following line of code for adding a search bar as a tableHeaderView:

self.tableView.tableHeaderView = self.searchController.searchBar

But my problem is hiding it like the iOS apps. On the iBooks app's search bar always behind of the Navigation Bar. I don't found any related documentations from the Apple's Documentations.

enter image description here enter image description here

Mannopson
  • 2,634
  • 1
  • 16
  • 32

1 Answers1

1

To hide this bar from the start, you can use setContentOffset, as demonstrated in this answer:

self.tableView.setContentOffset(CGPoint(x: 0, y: 44), animated: false)

This should hide the search bar from the start.

Community
  • 1
  • 1
Forest Kunecke
  • 2,160
  • 15
  • 32
  • Doesn't work. I have called your suggestion inside of the viewDidLoad method – Mannopson Mar 30 '17 at 02:50
  • `override func viewDidLoad() { super.viewDidLoad() DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) { self.tableView.setContentOffset(CGPoint.init(x: 0, y: 44), animated: false) } } ` This is works well. Thanks for your help – Mannopson Mar 30 '17 at 02:56
  • That's odd. I wonder why it works with a delay. Have you tried it without the delay an `animated: true`? – Forest Kunecke Mar 30 '17 at 02:57
  • Try putting it in `viewDidAppear` instead. [It occurs later in the lifecycle of the UIViewController](http://stackoverflow.com/a/22215127/1822214) – Forest Kunecke Mar 30 '17 at 03:00
  • Thanks very much! – Mannopson Mar 31 '17 at 02:46