gpt4 book ai didi

ios - 切换选项卡后 UIViewcontroller 消失 UITabBarController

转载 作者:行者123 更新时间:2023-11-28 14:49:30 24 4
gpt4 key购买 nike

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

enter image description here

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

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

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 的 View Controller :

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 = ""
}
}

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

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 synchronize] 在事务中调用 与在主线程中渲染多个动画有关。

我想知道这个 viewcontroller 可视化问题是否与此有关。所以我评论了 SearchController 类中的 searchController 实例化行:

self.navigationItem.searchController = searchController

现在,在我的第一个 View Controller 中,UITabBarController 在没有 UISearchController 的情况下也能完美运行。

我的问题是:

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

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

最佳答案

在您的 TabBarController 类中实现此功能:

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

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

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
}

关于ios - 切换选项卡后 UIViewcontroller 消失 UITabBarController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49969632/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com