Categories
ios swift uisearchcontroller

UISearchController won’t dismiss searchbar and overlap for iOS 8 Swift

I’m having problem with the UISearchController where if I have text in the searchbar and dismiss the VC it’s in, the searchBar won’t go away and just remain on screen overlapping everything in other VCs. Then it crashes if you hit the cancel button.

Have tried a few solutions on SO but none have worked. :/

self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.hidesNavigationBarDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.searchBarStyle = UISearchBarStyle.Minimal
controller.searchBar.barTintColor = UIColor(red: 243/255, green: 243/255, blue: 243/255, alpha: 1)
controller.searchBar.tintColor = UIColor.blackColor()
controller.definesPresentationContext = true
controller.edgesForExtendedLayout = UIRectEdge.None
self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0)
self.tableView.tableHeaderView = controller.searchBar
return controller
})()

Really appreciate any help on this!

UPDATE: So I kinda found a not-so-great solution which is to set .active = false in viewWillDisappear. However, the problem is there a searchBar artifact will show on the next/previous VC for a second before completely disappearing.