Search code examples
iosswiftuitableviewuisearchcontrollerios8.1

UISearchController result tableView not fit with window IOS 8.1


I have implemented UISearchController to search data of my app. it is working fine for ios 9.

But When i run the app on IOS 8.1 simulator following issues happens when i srart search.

enter image description here

Search result controller tableview is not fit with the window.

I implemented UISearchViewController as follows

override func viewDidLoad() {
    super.viewDidLoad()

    let searchResultControllerNetwork = HVNetworkSearchController()
    searchResultControllerNetwork.selectRowDelegate = self
    searchControllerNetwork = UISearchController(searchResultsController: searchResultControllerNetwork)
    let _ = self.searchControllerNetwork.view
    searchControllerNetwork.searchResultsUpdater = searchResultControllerNetwork
    searchControllerNetwork.searchBar.delegate = self
    searchControllerNetwork.searchBar.sizeToFit()
    self.view.addSubview(self.searchControllerNetwork.searchBar)
    self.definesPresentationContext = true

    //Other codes here
}

I could not find a solution from the internet and i decided to post the issue here.


Solution

  • Seems this is a IOS 8 bug. This happens because of following reason

    Search result controller does not have a navigation bar. IOS 8 use previous view controller tableview height.since there is not navigation bar it reduces from the bottom. How ever we can fix the issue for IOS 8 since it happens on IOS 8.

    I have done as follows

    @IBOutlet var tableviewBottomConstraint: NSLayoutConstraint!
    
    if (System version is less than 9.0) {
        tableviewBottomConstraint.constant = -(navigation bar height)
    }