由于我一直在使用swift4在Xcode内部开发一个选项卡式应用程序,我注意到一旦你有足够的标签并点击更多它就可以选择重新排序选项卡。但是,当我关闭应用程序时,它们会重置我可以通过任何方式保存订单,将标签更改为?
答案 0 :(得分:2)
使用下一个代码
对您的TabBarController进行子类化import UIKit
class MyTabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
restoreOrder()
}
override func tabBar(_ tabBar: UITabBar, didEndCustomizing items: [UITabBarItem], changed: Bool) {
let order = getCurrentOrder()
saveOrder(order: order)
}
private func getCurrentOrder() -> [Int] {
var order: [Int] = []
if let controllers = self.viewControllers {
for controller in controllers {
order.append(controller.tabBarItem.tag)
}
}
return order
}
private func saveOrder(order: [Int]) {
UserDefaults.standard.set(order, forKey: "TabBarItemsOrder")
}
private func restoreOrder() {
guard let order = UserDefaults.standard.value(forKey: "TabBarItemsOrder") as? [Int] else { return }
var reorderedControllers: [UIViewController] = []
if let controllers = self.viewControllers {
for tag in order {
for controller in controllers {
if controller.tabBarItem.tag == tag {
reorderedControllers.append(controller)
}
}
}
}
self.setViewControllers(reorderedControllers, animated: false)
}
}
为由Tab栏控制器管理的每个viewController标签栏项设置标记。