I also met the same problem in my application. It works great in iOS 10.
However, it will pop out of my view in iOS11 like this.

Also, I do not want to put my search bar into navigation mode.
So maybe you can consider my answer. There he is.
In the storyboard:

the code:
override func viewDidLoad() {
super.viewDidLoad()
self.setupSearchController()
}
func setupSearchController() {
self.searchResultController = UISearchController(searchResultsController: nil)
self.searchResultController.searchResultsUpdater = self
self.searchResultController.delegate = self
self.searchResultController.hidesNavigationBarDuringPresentation = false
self.searchResultController.dimsBackgroundDuringPresentation = false
self.searchResultController.searchBar.delegate = self
self.searchResultController.searchBar.searchBarStyle = .minimal
self.searchResultController.searchBar.tintColor = UIColor.white
self.phoneSearchView.searchBarContainer.addSubview(self.searchResultController.searchBar)
self.memberListTableView.tableHeaderView = self.phoneSearchView
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.searchResultController.searchBar.sizeToFit()
self.searchResultController.searchBar.frame.size.width = self.phoneSearchView.searchBarContainer.frame.size.width
self.searchResultController.searchBar.frame.size.height = self.phoneSearchView.searchBarContainer.frame.size.height
}
Now it works like a charm.

source
share