12

I have a UITableVIewController that implements the UISearchBarDelegate, the view is embedded inside a Navigation Controller.

    class FacilityTableViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate, AmenityFilterDelegate {

        // MARK: - Public Variables

        var targetFacilities = [Int]()
        var searchController: UISearchController = UISearchController(searchResultsController: nil)

        // MARK: - Private Variables

        private var viewModel: FacilityTableViewModel!
        private let parkGreenColor = UIColor(red: 73/255, green: 136/255, blue: 84/255, alpha: 1)
        private var showEmptyMessage = false

        // MARK: - View Lifecycle

        /**
        Setup view after loading
        */
        override func viewDidLoad() {
            super.viewDidLoad()

            trackScreenView("Facility Table View")

            if targetFacilities.isEmpty {
                viewModel = FacilityTableViewModel()
            } else {
                viewModel = FacilityTableViewModel(facilityIds: targetFacilities)
            }

            // Seup search controller
            searchController.searchResultsUpdater = self
            searchController.dimsBackgroundDuringPresentation = false
            searchController.hidesNavigationBarDuringPresentation = false
            searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, searchController.searchBar.frame.origin.y, searchController.searchBar.frame.size.width, 44)
            searchController.searchBar.tintColor = UIColor.whiteColor()
            searchController.searchBar.barTintColor = parkGreenColor
            searchController.searchBar.translucent = false

            self.definesPresentationContext = true

            tableView.tableHeaderView = searchController.searchBar
        }

Before Tapping on Search

I found that when I disabled the Navigation Bar's Translucent property the Search Box shifts it's position down.

After Tapping on Search

If I set the definesPresentationContext = false then the Search Bar does not shift down, however if I enter text into the search box and select one of the results the resulting modal window cannot open. I get the following error:

Search Results

    2015-03-17 15:06:56.101 VB ParkFinder[16368:2667719] Warning: Attempt to present <UINavigationController: 0x7fa2f9ced930>  on <VB_ParkFinder.FacilityTableViewController: 0x7fa2f9c27ba0> which is already presenting (null)

Below is my segue code:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let navController = segue.destinationViewController as UINavigationController
        if segue.identifier == "facilityDetailsSegue" {
            let detailsViewController = navController.childViewControllers.last as FacilityDetailsViewController

            if let indexPath = tableView.indexPathForSelectedRow() {
                var facilityId: Int
                if searchController.active {
                    facilityId = viewModel.idForSearchResultsAtIndexPath(indexPath)
                } else {
                    facilityId = viewModel.idForFacilityAtIndexPath(indexPath)
                }

                detailsViewController.currentFacilityId = facilityId
            }
        } else if segue.identifier == "FilterPopover" {
            let aftvc = navController.childViewControllers.last as AmenityFilterTableViewController
            aftvc.delegate = self
        }
    }

I am lost as to what to do. I want to leave the Navigation Bar with translucency turned off and I need to be able to launch a modal window from the search results. Any thoughts on how to accomplish this?

g0ld2k
  • 519
  • 4
  • 17
  • 1
    possible duplicate of [Strange UISearchDisplayController view offset behavior in iOS 7 when embedded in navigation bar](http://stackoverflow.com/questions/20731360/strange-uisearchdisplaycontroller-view-offset-behavior-in-ios-7-when-embedded-in) – M. Porooshani Apr 29 '15 at 12:32

1 Answers1

6

I was having the same problem, take a look at

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

This solved my problem.

Guess it could be marked as duplicate, not sure how to do that.

Community
  • 1
  • 1
Ade
  • 631
  • 5
  • 8
  • Please, do not post links. Instead write the actual solution here. Link can die. – Raphael Aug 26 '16 at 10:54
  • 3
    To save you time: the link is generally about setting "Under Top Bars" and "Under Opaque Bars" to TRUE for the view controller that has table view with the search bar causing problems. – raven_raven Feb 03 '17 at 09:13