如何在切换视图时使UISearchController处于非活动状态

时间:2021-04-28 14:45:06

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.

在NavigationController中,我们有一个UITabBarController(带有3个选项卡)和一个带有UISearchController的UIViewController。

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.

如果我们将UISearchController保持活动状态并切换到另一个视图,则会出现错误,当我们返回搜索视图时,整个屏幕都是黑色的。但是,当UISearchController未激活且我们切换视图时,这不会发生。

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.)

我们试图在控制视图之间设置控制器不活动;但是,当UISearchController处于活动状态时,不会调用任何segueing事件(viewWillDissapear,viewWillAppear等没有显示日志打印)

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

在其他线程上,我们尝试设置self.definesPresentationContext = true,但这不起作用。

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

有没有其他人有这个问题或知道如何解决它?

2 个解决方案

#1


2  

Try to set the searchbarController active like this

尝试将searchbarController设置为活动状态

self.resultSeachController.active = false

before you move on the next View

在继续下一个View之前

#2


0  

I faced the same problem and solved it as follows:

我遇到了同样的问题并解决了如下:

I extended UITabBarController and created a custom class TabBarController

我扩展了UITabBarController并创建了一个自定义类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

在那个类中我实现了它的didSelectItem方法,在那个方法中我调用了一个关闭搜索控制器的视图控制器的方法

// 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.

viewControllers是UITabBarController中的一个数组,它保存所有属于UITabBarController的视图控制器,'selectedIndex'是显示的Tab(和视图控制器)的索引,因此可以访问具有searchController活动的viewController 。

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.

在我的应用程序中,所有视图控制器都是名为CommonViewController的根类的子类,其中我放置了所有视图控制器共有的所有变量和方法,例如所有搜索功能。因此,我只是检查搜索控制器是否处于活动状态,如果是,我调用一个使其处于非活动状态并执行其他相关清理的方法。

#1


2  

Try to set the searchbarController active like this

尝试将searchbarController设置为活动状态

self.resultSeachController.active = false

before you move on the next View

在继续下一个View之前

#2


0  

I faced the same problem and solved it as follows:

我遇到了同样的问题并解决了如下:

I extended UITabBarController and created a custom class TabBarController

我扩展了UITabBarController并创建了一个自定义类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

在那个类中我实现了它的didSelectItem方法,在那个方法中我调用了一个关闭搜索控制器的视图控制器的方法

// 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.

viewControllers是UITabBarController中的一个数组,它保存所有属于UITabBarController的视图控制器,'selectedIndex'是显示的Tab(和视图控制器)的索引,因此可以访问具有searchController活动的viewController 。

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.

在我的应用程序中,所有视图控制器都是名为CommonViewController的根类的子类,其中我放置了所有视图控制器共有的所有变量和方法,例如所有搜索功能。因此,我只是检查搜索控制器是否处于活动状态,如果是,我调用一个使其处于非活动状态并执行其他相关清理的方法。