UITabBarContoller,不时消失最后一个项目

时间:2018-06-30 08:46:56

标签: 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知道会是什么吗?谢谢

1 个答案:

答案 0 :(得分:0)

问题在于此选项卡的保存顺序为“用户默认设置”。 我有7个控制器,但是当尝试保存它时,保存的只有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)
        }
    }
}