3

I am using the UISearchController alongside its UISearchBar. The UISearchController alongside its UITableView is placed inside a subview in my main view.

My goal is to lock the UISearchBar in its place so when its active, it does not move. Right now the following is happening:

enter image description here enter image description here

I have searched this issue extensively and have tried the following such as:

  1. self.searchController.hidesNavigationBarDuringPresentation = false, which just abruptly moves the search bar to the middle of the screen.

  2. definesPresentationContext = true, which does not change behavior

  3. self.extendedLayoutIncludesOpaqueBars = true, which does not change behavior

  4. Using the UIBarPositioningDelegate method mentioned here, which strangely cannot be translated to Swift because I cannot insert the AnyObject<UIBarPositioning> as it is in the Objective C version and still have it recognized as the override function

Any suggestion is appreciated.

Edit:

Here is the setup for the search items:

    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    definesPresentationContext = true
    searchTableView.tableHeaderView = searchController.searchBar
daspianist
  • 5,336
  • 8
  • 50
  • 94
  • look at this link.. especially at last answer https://stackoverflow.com/questions/31702413/how-to-prevent-search-bar-from-disappearing-on-scroll-ios-swift – Gagan_iOS Sep 22 '17 at 17:07
  • Thanks for posting this. While this is good at keeping the UISearchBar at the same location, it still animates and hides into my view. – daspianist Sep 22 '17 at 17:16
  • Are you adding search bar in Story board? If yes, then plz create a searchbar in your code & then add it on tableview header. – Gagan_iOS Sep 22 '17 at 17:17
  • I am not. Everything is done programmatically. I have edited the question above – daspianist Sep 22 '17 at 17:21
  • 1
    @daspianist for what it's worth you can try having a `UIViewController` then at the top have your header view (the one with the tab selecting) and then your `UISearchBar` under it and then finally, have a container view that embeds a `UITableViewController`. If you do it this way I can guarantee it wont move. Although not sure how elegant it is. (I have this exact same type of UI in my project) – TNguyen Sep 25 '17 at 19:45
  • That's a interesting suggestion. I'll give that a try and see what the outcome could be – daspianist Sep 25 '17 at 20:04
  • Could you provide us your code that you use to embed the SearchBar and the TableView as a subview? Are you using auto-layout? – Artheyn Sep 27 '17 at 09:51

1 Answers1

1

Set your TableView's Style property to Plain in the Storyboard and use the SearchBar as the section header view. Along with the following code, I commented out tableView.tableHeaderView = searchController.searchBar in Apple's "Table Search with UISearchController" example and it worked fine.

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return searchController.searchBar.frame.size.height
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return searchController.searchBar
}
Matt H
  • 6,422
  • 2
  • 28
  • 32
  • This seems like a good execution. However, when I tried it it just lodges the searchBar above the view (https://imgur.com/a/wHf6A) – daspianist Sep 22 '17 at 20:58
  • I don’t follow the effect you are trying to achieve, then. @daspianist – Matt H Sep 23 '17 at 20:57
  • Hey Matt, my goal is to simply not have the searchBar move from its original position when the user is typing, as seen in the question body's second image. In this situation 1. the searchBar is overlapping parts of the first cell and 2. it is bumping other views upwards. – daspianist Sep 24 '17 at 22:08