• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-11 20:18:26 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我像往常一样有我的自定义 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 = "erformance"
    performanceNavigationController.tabBarItem.image = UIImage(named: "erformance")

    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 = "lan"
    coachNavigationController.tabBarItem.image = UIImage(named: "lan")

    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 知道它是什么吗?谢谢



Best Answer-推荐答案


问题在于将此选项卡的顺序保存到用户默认值。 我有 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap