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

标签 ios swift

我像往常一样有我的自定义 TabBarController,它包含 8 个 viewController

class STTabBarController: UITabBarController,UITabBarControllerDelegate {

let tabBarOrderKey = "tabBarOrderKey"
private var messangerNavigationController: UINavigationController!

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

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

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

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

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

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


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

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

    self.viewControllers = [clientNavigationController ,performanceNavigationController,calculatorsNavigationController, coachNavigationController,industryProfessionalsNavigationController,agentResourcesNavigationController,openHouseNavigationController, settingsNavigationController]

    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 = [UIViewController]()
    if let initialViewControllers = viewControllers {
        if let tabBarOrder = UserDefaults.standard.object(forKey: tabBarOrderKey) as? [Int] {
            for tag in tabBarOrder {
                newViewControllerOrder.append(initialViewControllers[tag])
            }
            setViewControllers(newViewControllerOrder, animated: false)
        }
    }
}

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

        }

        UserDefaults.standard.set(orderedTagItems, forKey: tabBarOrderKey)
    }
}

当我在不同的设备上启动时,我遇到了这个问题,它有时会隐藏“更多”选项卡中的设置(最后一个)项目。这看起来有点荒谬,因为代码如您所见简单明了,但我不知道这里可能出什么问题。 smb 知道它会是什么吗?谢谢

最佳答案

问题在于将此选项卡的顺序保存到用户默认值。 我有 7 个 Controller ,但在尝试保存时,只保存了 6 个。 下面是保存函数的最终代码:

func getSavedTabBarItemsOrder() {
    var newViewControllerOrder = [UIViewController]()
    if let initialViewControllers = viewControllers {
        if let tabBarOrder = UserDefaults.standard.object(forKey: tabBarOrderKey) as? [Int] {
            for tag in tabBarOrder {
                newViewControllerOrder.append(initialViewControllers[tag])
            }
            let difference = Set(initialViewControllers).subtracting(newViewControllerOrder)
            newViewControllerOrder.append(contentsOf: difference)
            setViewControllers(newViewControllerOrder, animated: false)
        }
    }
}

关于ios - UITabBarController,不时消失最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51113315/

相关文章:

iphone - 导致崩溃的通知监听器

android - flutter 应用程序编译错误 :too few positional arguments: 1 required, 0 给出。前导 = IconButton(

swift - GIDSignIn.sharedInstance : sharedInstance not a function?

ios - 更改 Coreplot 图形的刻度标签

ios - 兑换 iOS App Store 促销代码的 URL 是什么

ios - 如何从浏览器打开iOS应用程序?

ios - 如何将 CGImage 转换为 OTVideoFrame

ios - 用于处理登录的 facebookSDK、Swift3 和 AppDelegate

arrays - 类型 Any 在 Swift 3.0 中没有下标成员错误?

swift - 更改 imageView 的色调颜色时性能不佳