Jared Katzman
Jared Katzman

Reputation: 91

How to make UISearchController inactive when switching views

A little background on the setting of our views:

Inside a NavigationController, we have a UITabBarController (with 3 tabs) with a UIViewController that has a UISearchController.

There is an error that if we leave the UISearchController active and switch to another view, when we return to the search view the entire screen is black. However, when the UISearchController is not active and we switch views this does not happen.

We have tried to set the controller to not be active when segueing between views; however, when the UISearchController is active none of the segueing events get called (no log prints appear from viewWillDissapear, viewWillAppear, etc.)

Looking on other threads, we tried setting self.definesPresentationContext = true but that does not work.

Has anyone else had this problem or know how to fix it?

Upvotes: 9

Views: 2399

Answers (2)

Zvi
Zvi

Reputation: 2424

I faced the same problem and solved it as follows:

I extended UITabBarController and created a custom class TabBarController

class TabBarController: UITabBarController  {

In that class I implemented its didSelectItem method, and in that method I called a method of the view controller that closes the search controller

// UITabBarDelegate
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
    let vc = viewControllers![selectedIndex] as! CommonViewController
    if vc.searchController.active {
        vc.searchBarCancelButtonClicked_NoReload()
    }
}

viewControllers is an array in UITabBarController that keeps all the view controllers belonging to the UITabBarController, and 'selectedIndex' is the index of the Tab (and the view controller) which was displayed, and thus one can get to the viewController that has the searchController active.

In my app all the view controllers are subclasses of a root class named CommonViewController where I put all the vars and methods that are common to all view controllers, such as all the search functionality. Therefore I simply check if the search controller is active and if it is I call a method that makes it inactive and does some other related cleanup.

Upvotes: 0

Hieu Duc Pham
Hieu Duc Pham

Reputation: 1074

Try to set the searchbarController active like this

self.resultSeachController.active = false

before you move on the next View

Upvotes: 2

Related Questions