菜鸟教程小白 发表于 2022-12-11 20:18:26

ios - UITabBarController,时不时消失最后一项


                                            <p><p>我像往常一样有我的自定义 <code>TabBarController</code>,其中包含 8 个 <code>viewController</code>。</p>

<pre><code>class STTabBarController: UITabBarController,UITabBarControllerDelegate {

let tabBarOrderKey = &#34;tabBarOrderKey&#34;
private var messangerNavigationController: UINavigationController!

override func viewDidLoad() {
    super.viewDidLoad()
    self.delegate = self
    configureViewControllers()
    setUpTabBarItemTags()
    getSavedTabBarItemsOrder()
}

func configureViewControllers() {
    let clientsController = STClientsViewController(nibName: &#34;STClientsViewController&#34;, bundle: nil)
    let clientNavigationController = UINavigationController(rootViewController: clientsController)
    clientsController.title = &#34;Clients&#34;
    clientNavigationController.tabBarItem.image = UIImage(named: &#34;Client&#34;)

    let openHouseController = STOpenHouseViewController(nibName: &#34;STOpenHouseViewController&#34;, bundle: nil)
    let openHouseNavigationController = UINavigationController(rootViewController: openHouseController)
    openHouseController.title = &#34;Open House&#34;
    openHouseNavigationController.tabBarItem.image = UIImage(named: &#34;OpenHouse&#34;)

    let performanceController = STChartsViewController(nibName: &#34;STChartsViewController&#34;, bundle: nil)
    let performanceNavigationController = UINavigationController(rootViewController: performanceController)
    performanceController.title = &#34;Performance&#34;
    performanceNavigationController.tabBarItem.image = UIImage(named: &#34;Performance&#34;)

    let calculatorsController =STCalculatorsViewController(nibName: &#34;STCalculatorsViewController&#34;, bundle: nil)
    let calculatorsNavigationController = UINavigationController(rootViewController: calculatorsController)
    calculatorsController.title = &#34;Calculators&#34;
    calculatorsNavigationController.tabBarItem.image = UIImage(named: &#34;Calculators&#34;)
    let storyBoard:UIStoryboard = UIStoryboard(name: &#34;Main&#34;, bundle: nil)
    let communityViewController = storyBoard.instantiateViewController(withIdentifier: &#34;Navigation&#34;)
    communityViewController.title = &#34;Community&#34;
    communityViewController.tabBarItem.image = UIImage (named:&#34;Community&#34;)
    let industryProfessionalsController = STIndustryProfessionalsViewController(nibName: &#34;STIndustryProfessionalsViewController&#34;, bundle: nil)
    let industryProfessionalsNavigationController = UINavigationController(rootViewController: industryProfessionalsController)
    industryProfessionalsController.title = &#34;Vendors&#34;
    industryProfessionalsNavigationController.title = &#34;Vendors&#34;
    industryProfessionalsNavigationController.tabBarItem.image = UIImage(named: &#34;Industry-professionals&#34;)

    let agentResourcesController = STAgentResourcesViewController(nibName: &#34;STAgentResourcesViewController&#34;, bundle: nil)
    let agentResourcesNavigationController = UINavigationController(rootViewController: agentResourcesController)
   agentResourcesController.title = &#34;Resources&#34;
    agentResourcesNavigationController.title = &#34;Resources&#34;
    agentResourcesNavigationController.tabBarItem.image = UIImage(named: &#34;Agent-Resources&#34;)


    let settingsController = STSettingsViewController(nibName: &#34;STSettingsViewController&#34;, bundle: nil)
    let settingsNavigationController = UINavigationController(rootViewController: settingsController)
    settingsController.title = &#34;Settings&#34;
    settingsNavigationController.tabBarItem.image = UIImage(named: &#34;Settings&#34;)

    let coachController = STCoachsCornerViewController(nibName: &#34;STCoachsCornerViewController&#34;, bundle: nil)
    let coachNavigationController = UINavigationController(rootViewController: coachController)
    coachController.navigationItem.title = &#34;Action Plan&#34;
    coachNavigationController.tabBarItem.title = &#34;Plan&#34;
    coachNavigationController.tabBarItem.image = UIImage(named: &#34;Plan&#34;)

    self.viewControllers =

    tabBar.isTranslucent = false
    let topBorder = CALayer()
    topBorder.frame = CGRect(x: 0, y: 0, width: 1000, height: 0.5)
    topBorder.backgroundColor = UIColor.returnRGBColor(r: 229, g: 231, b: 235, alpha: 1).cgColor
    tabBar.layer.addSublayer(topBorder)
    tabBar.clipsToBounds = true
}

func setUpTabBarItemTags() {
    var tag = 0
    if let viewControllers = viewControllers {
      for view in viewControllers {
            view.tabBarItem.tag = tag
            tag += 1
      }
    }
}

func getSavedTabBarItemsOrder() {
    var newViewControllerOrder = ()
    if let initialViewControllers = viewControllers {
      if let tabBarOrder = UserDefaults.standard.object(forKey: tabBarOrderKey) as? {
            for tag in tabBarOrder {
                newViewControllerOrder.append(initialViewControllers)
            }
            setViewControllers(newViewControllerOrder, animated: false)
      }
    }
}

func tabBarController(_ tabBarController: UITabBarController, didEndCustomizing viewControllers: , changed: Bool) {
    var orderedTagItems = ()
    if changed {
      for viewController in viewControllers {
            let tag = viewController.tabBarItem.tag
            orderedTagItems.append(tag)

      }

      UserDefaults.standard.set(orderedTagItems, forKey: tabBarOrderKey)
    }
}
</code></pre>

<p>当我在不同的设备上启动时遇到了这个问题,有时它会在“更多”选项卡中隐藏设置(最后一个)项目。这看起来有点荒谬,因为代码就像你看到的那样简单明了,而我没有知道这里有什么问题。
smb 知道它是什么吗?谢谢</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>问题在于将此选项卡的顺序保存到用户默认值。
我有 7 个 Controller ,但是当试图保存它时,只保存了 6 个。
这是保存功能的最终代码:</p>

<pre><code>func getSavedTabBarItemsOrder() {
    var newViewControllerOrder = ()
    if let initialViewControllers = viewControllers {
      if let tabBarOrder = UserDefaults.standard.object(forKey: tabBarOrderKey) as? {
            for tag in tabBarOrder {
                newViewControllerOrder.append(initialViewControllers)
            }
            let difference = Set(initialViewControllers).subtracting(newViewControllerOrder)
            newViewControllerOrder.append(contentsOf: difference)
            setViewControllers(newViewControllerOrder, animated: false)
      }
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UITabBarController,时不时消失最后一项,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/51113315/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/51113315/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UITabBarController,时不时消失最后一项