0

I embedded a viewcontroller(BlueViewController) in a navigation controller.

the blueViewController has a UISearchController that set as the blueViewController's tableView headerView.

the searchController works fine except when it's active, it shift up and hide behind the UINavigationController(or somewhere)

I've tried to add the search controller's searchBar as a subview to the viewController, or positions a UIView and assigned the searchBar to that UIView, none of these works

I have tried this link, doesn't work Strange UISearchDisplayController view offset behavior in iOS 7 when embedded in navigation bar

here is the searchController in BlueViewController

let searchController:UISearchController = {
    let controller = UISearchController(searchResultsController: nil)
    controller.hidesNavigationBarDuringPresentation = false
    controller.obscuresBackgroundDuringPresentation = false
    controller.searchBar.inputAssistantItem.leadingBarButtonGroups = []
    controller.searchBar.inputAssistantItem.trailingBarButtonGroups = []
    return controller
}()

In viewDidLoad

 searchController.searchResultsUpdater = self
    searchController.obscuresBackgroundDuringPresentation = false
    definesPresentationContext = true

you can also find entire project here https://github.com/QiquanLu/TestNavigationWithSearchController

Any hint would be appreciated, thanks!

Qiquan Lu
  • 345
  • 1
  • 3
  • 8

1 Answers1

0

For iOS 11 and above, you should be setting the searchController property on BlueViewController's navigationItem like so:

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.searchController = searchController
}

Don't add it as the tableView's header.

Mark
  • 6,777
  • 3
  • 41
  • 63
  • Thanks, but the navigation items just not a place I want the search bar to be. – Qiquan Lu Apr 25 '19 at 12:54
  • Ah, I see. Not using the search controller this way might end up with weird bugs like the one you're seeing. Sorry I couldn't be of more help! – Mark Apr 25 '19 at 13:34