Strange UISearchDisplayController view offset behavior in iOS 7 when embedded in navigation bar

15,774

Solution 1

I remember running into the same exact problem that you are observing.There could be a couple of solutions you can try.

  • If you are using storyboards You should click on the view controller or TableView Controller which you have set up for your tableview and go to its attribute inspector and look under ViewController section and set the Extend Edges section to be under Top Bars.

  • If you are not using storyboards you can manually set the settings using the viewcontrollers edgesForExtendedLayout property and that should do the trick. I was using storyboards.

Solution 2

In my case, using storyboards, I had to check both Under Top Bars and Under Opaque Bars and leave Under Bottom Bars unchecked.

Solution 3

In my case, I actually had to uncheck all the Extended Edges boxes (essentially the same as programmatically setting Extended Edges to UIRectEdgeNone I believe) in my Storyboard in order to stop my search bar from offsetting itself. Thank you guys!

Solution 4

definesPresentationContext = true

override func viewDidLoad() {
        super.viewDidLoad()

        searchController = UISearchController(searchResultsController: nil)
        searchController.searchResultsUpdater = self
        searchController.hidesNavigationBarDuringPresentation = false

        searchController.dimsBackgroundDuringPresentation = true
        searchController.searchBar.searchBarStyle = UISearchBarStyle.Prominent
        self.tableView.tableHeaderView = searchController.searchBar

        definesPresentationContext = true

or see UISearchBar presented by UISearchController in table header view animates too far when active

Solution 5

My problem was just Adjust scroll view inserts. After change to false I didn't have problem

Share:
15,774

Related videos on Youtube

d370urn3ur
Author by

d370urn3ur

Updated on June 30, 2022

Comments

  • d370urn3ur
    d370urn3ur about 2 years

    I am building an iOS 7-only app. I am trying to set a UISearchDisplayController into the navigation bar.

    I have it set up like this: In the storyboard, I added a "Search Bar and Search Display Controller" to my view controller's view, and set it at (0,0) relative to the top layout guide. I set constraints to pin to left, top and right. (I played with the constraints, i removed them completely, it doesn't matter) On top of that I have my Table view. When I added the search bar to the view in the storyboard, it automatically setup outlets for searchDisplayController and searchBar delegate. In code I have self.searchDisplayController.displaysSearchBarInNavigationBar = YES; I have two problems:

    1) Without any buttons showing for the search bar (Interface builder -> select search bar -> Options: none selected) the search bar is in the middle of the screen:

    enter image description here

    If I click on the navigation bar, it starts editing the search bar:

    enter image description here

    notice also that the dark overlay appears to be offset from the navigation bar. It seems to me that the space is the same height as the navigation bar. Like it has been shifted down by that much. Also, when it displays the search results, the top of the content view is shifted down by the same amount (more pictures follow), which brings me to the second problem.

    2) I messed around with it for a while and decided to check the option to have it show the cancel button. Now I have the search bar embedded in the nav bar correctly, but the overlay is still shifted down:

    enter image description here

    Again, when the search results table view appears, it is shifted down by the same amount (notice the scroll bar on the right side):

    enter image description here

    Even more bizarrely, I set a border on the search display controller's tableview layer, and it appears correct:

    enter image description here

    I have never used the UISearchDisplayController before and I unfamiliar with how to set it up, but functionally it works fine. I have read some other similar posts but the only advice is to hack it up by adjusting frames and setting manual offsets. I'd prefer to know what is causing this, is it a bug? Something I'm doing wrong? If it's a bug I can wait for a fix. It seems like such a basic thing that a thousand people must have done without any problem so I feel like I'm not setting it up correctly somehow. Thanks for you input.

  • d370urn3ur
    d370urn3ur over 10 years
    thanks, that worked. I still wish I could figure out why the search bar doesn't work without the cancel button, but this is close enough
  • hackamanshu
    hackamanshu over 10 years
    I have it working without the Cancel Button, what exactly is happening when you don't add the Cancel button.
  • d370urn3ur
    d370urn3ur over 10 years
    if you look at the 1st and 2nd pictures in my question you can see it. Basically, with the cancel button, the search bar embeds correctly in the nav bar. Without the cancel button, the search bar is displaced to the center of the screen, on top of the table view
  • hackamanshu
    hackamanshu over 10 years
    Ok, I simply added the SearchBar with SearchDisplay Controller to my viewController (didn't touch constraints at all) and I don't think you need to set origin to (0,0) since the searchBar will be living on the navigationBar. Also, to me it looks like an autolayout issue. Try unchecking the autolayout option for the View Controller in the File Inspector to see if the behavior changes.
  • d370urn3ur
    d370urn3ur over 10 years
    it's strange, I finally got it to work by removing the searchbar with searchdisplay controller from the viewcontroller's view and placing it OUTSIDE the view hierarchy completely. I've never done that before, but it worked. That also had the added effect of adding some kind of automatic padding to the table view so that it is placed below the nav bar. Otherwise, I have to add the padding myself (64px) to offset the nav bar.
  • hackamanshu
    hackamanshu over 10 years
    I think its because you must have been using a UITableViewController and therefore putting the SearchBar inside the View hierarchy was causing concerns. Well I'm glad you got it working :)
  • Cedrick
    Cedrick over 8 years
    Also don't forget to see if extendedLayoutIncludesOpaqueBars is applicable for your specific situation.
  • Jonathan Brown
    Jonathan Brown over 8 years
    Had to check Under Opaque Bars to get it to work for me
  • nsuinteger
    nsuinteger over 8 years
    if your navigationbar is set to opaque, tick the 'extend under opaque bars' as well.
  • Stan Mots
    Stan Mots almost 7 years
    Same here...in 2017