UIViewcontroller 在切换标签 UITabBarController 后消失 [英] UIViewcontroller disappear after switch tabs UITabBarController

查看:22
本文介绍了UIViewcontroller 在切换标签 UITabBarController 后消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用 UITabBarController 的应用程序,突然一个 tabItem 停止出现.然后我开始调查并最终解决了与 UISearchControllerUITabBarController 相关的问题.

I'm working on an app that uses UITabBarController and sudenlly one tabItem stops to appear. Then I start to investigate and end up with a problem related with UISearchController and UITabBarController.

要隔离问题,请构建一个简单的应用程序来演示情况.

To isolate the problem a build a simple app to demostrate the situation.

这就是我在 didFinishLaunchingWithOptions 中实例化 TabBar 的方式:

This is how I instanciate the TabBar at didFinishLaunchingWithOptions :

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.backgroundColor = .white

    let first:SearchController = {
        let sc = SearchController()
        sc.tabBarItem = UITabBarItem(title: "First", image: UIImage(named:"iphone"), tag: 0)
        return sc
    }()

    let second:SecondViewController = {
        let s = SecondViewController()
        s.tabBarItem = UITabBarItem(title: "Second", image: UIImage(named:"iphone"), tag: 1)
        return s
    }()

    let tabBar = UITabBarController()
    let controllers = [first, second]
    tabBar.viewControllers = controllers.map {
        UINavigationController(rootViewController: $0)
    }


    self.window?.rootViewController = tabBar
    self.window?.makeKeyAndVisible()
    return true
}

这是带有 UISearchController 的视图控制器:

This is the viewController with UISearchController:

class SearchController: UIViewController {
    var matchingItems:[String] = [] {
        didSet{
            self.tableView.reloadData()
        }
    }

    lazy var searchController:UISearchController = UISearchController(searchResultsController: nil)

    lazy var tableView: UITableView = { [unowned self] in
        let tv = UITableView(frame: CGRect.zero, style: .grouped)
        tv.translatesAutoresizingMaskIntoConstraints = false
        tv.delegate = self
        tv.dataSource = self
        tv.register(UITableViewCell.self, forCellReuseIdentifier: "id")
        return tv
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "First"
        view.addSubview(self.tableView)
        self.navigationItem.searchController = searchController
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.perform(#selector(showKeyboard), with: nil, afterDelay: 0.1)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        searchController.searchBar.resignFirstResponder()
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        let layout = self.view.safeAreaLayoutGuide
        tableView.topAnchor.constraint(equalTo: layout.topAnchor).isActive = true
        tableView.bottomAnchor.constraint(equalTo: layout.bottomAnchor).isActive = true
        tableView.rightAnchor.constraint(equalTo: layout.rightAnchor).isActive = true
        tableView.leftAnchor.constraint(equalTo: layout.leftAnchor).isActive = true
    }

    @objc func showKeyboard() {
        self.searchController.searchBar.becomeFirstResponder()
        self.searchController.searchBar.text = ""
    }
}

在运行时,当系统完成渲染这个视图控制器时,调试控制台打印:

In run time, when system finished rendering this view controller, the debug console print this:

SearchBar+TabBar[15454:895018] [MC] Reading from private effective user settings.
2018-04-22 12:07:19.595887-0300 SearchBar+TabBar[15454:895018] +[CATransaction synchronize] called within transaction
2018-04-22 12:07:19.608090-0300 SearchBar+TabBar[15454:895018] +[CATransaction synchronize] called within transaction
2018-04-22 12:07:19.608269-0300 SearchBar+TabBar[15454:895018] +[CATransaction synchronize] called within transaction
2018-04-22 12:07:19.608516-0300 SearchBar+TabBar[15454:895018] +[CATransaction synchronize] called within transaction

在stackoverflow上搜索发现这个消息+[CATransaction同步]在事务中调用与在主线程中渲染多个动画有关.

Searching on the stackoverflow I found that this message +[CATransaction synchronize] called within transaction is related to rendering more than one animation in the main thread.

我想知道这个 viewcontroller 可视化问题是否与此有关.所以我在 SearchController 类中注释了 searchController 实例化行:

I'm wondering if this viewcontroller visualisation problem is related to that. So I commented the searchController instantiation line in SearchController class:

self.navigationItem.searchController = searchController

现在 UITabBarController 在我的第一个视图控制器中没有 UISearchController 可以完美运行.

Now the UITabBarController works perfectly without the UISearchController in my first view controller.

我的问题是:

  1. 是否有另一种方法来实例化 UISearchController 以避免这个?
  2. 如果是,我该怎么做?

带有示例代码的 GitHub 存储库:示例代码

GitHub repository with the sample code: sample code

推荐答案

在你的 TabBarController 类中实现这个功能:

In Your TabBarController class implement this function:

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool

在此函数中,您应该检查之前是否选择了带有 searchBar 的 Controller 并使 SearchController 处于非活动状态

In this function you should check if Controller with searchBar was selected before and make SearchController inactive

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    if (tabBarController.selectedIndex == ControllerWithSearchBarIndex) {
        ((viewControllers![ControllerWithSearchBarIndex] as! UINavigationController).viewControllers.first! as! ControllerWithSearchBarIndex).searchController.isActive = false
    }
    return true
}

这篇关于UIViewcontroller 在切换标签 UITabBarController 后消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆